0

What is the “var” keyword all about???🤔

What are some practical/functional uses where “var” would be the BEST data type to use?

9th May 2018, 4:43 AM
Marcos
Marcos - avatar
7 Respuestas
+ 5
This is with respect to DRY and reducing cognitive load as most of the time the data type can be inferred from the variable name. If it's not the case, you got 1 more problem now. 😉
9th May 2018, 6:16 AM
Zephyr Koo
Zephyr Koo - avatar
+ 5
Marcos It's ok. DRY refers to Don't Repeat Yourself as we always encourage reuse and simplification (KISS · Keep It Short & Simple) in programming context. As mentioned by Lukas Weber, it's definitely easier to read var lookup = new Dictionary<string, string>(); than Dictionary<string, string> lookup = new Dictionary<string, string>(); Regarding the problem I've mentioned, it is about the readability of the program. 😉
9th May 2018, 3:42 PM
Zephyr Koo
Zephyr Koo - avatar
+ 2
Definitely not when creating variables, that is for sure. But! when ypu create a foreach loop and you are not perfetly sure what is the type of one item of, for example, dictionary or map, you just put a var keyword to save a headache, but everyone knoews what it is about. Widely used in that way
9th May 2018, 5:14 AM
Paul
+ 1
Some uses: 1: Shortening very long typenames var a = BuildComplexType(); instead of ComplexTypeWithAVeryLongName a = BuildComplexType(); 2: Not caring about the exact type var mult = 1-1/(7*9) return a*mult; 3: Simplifying foreach loops foreach(var element in collection)
9th May 2018, 10:53 AM
Lukas Weber
Lukas Weber - avatar
+ 1
Donna, I was asking what “var” would be BEST used for, as opposed to other data types. 😌Thank you for the comment anyway, and thank you to everyone else for the responses.😊 Zephyr, what problem? Keep in mind, I’m not advanced in coding. I don’t even know what DRY stands for. 😕
9th May 2018, 2:39 PM
Marcos
Marcos - avatar
0
Ok, cool. Thank you So much for elaborating Zephyr.🙂
9th May 2018, 3:59 PM
Marcos
Marcos - avatar