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 Respostas
+ 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.đ