0
What is the “var” keyword all about???🤔
What are some practical/functional uses where “var” would be the BEST data type to use?
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. 😉
+ 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. 😉
+ 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
+ 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)
+ 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. 😕
0
Ok, cool. Thank you
So much for elaborating Zephyr.🙂