It seems that more and more C# code I read uses the var type identifier:
foreach (var itemChange in ItemChanges)
{
//...
}
instead of explicitly stating the type:
foreach (ItemChange itemChange in ItemChanges)
{
//...
}
even when the type is known.
I am still using the latter explicit version just because I think someone reading it later will more quickly understand which type a variable is than if you use var.
But is there any technical reason to use one or the other?
There will be times when you need var, like when you need a temp variable to be available within the scope of a block inside of a function, otherwise, preferring let over var will help developers with naming conflicts.
Using var is lazy. While var is certainly easier to type than Dictionary<int,IList> , if the variable isn't named well, you'd never know what it refers to. Using var makes it hard to know what type the underlying variable actually is. Again, a properly named variable speaks for itself.
Only those declared with var are "local". Using var outside a function is optional; assigning a value to an undeclared variable implicitly declares it as a global variable (it is now a property of the global object).
By using “var”, you are giving full control of how a variable will be defined to someone else. You are depending on the C# compiler to determine the datatype of your local variable – not you. You are depending on the code inside the compiler – someone else's code outside of your code to determine your data type.
There is no technical reason. If the type cannot be inferred at compile time then the code will not compile.
You are right in stating there are instances where it may be better to use an explicit type for readabilty, e.g.
var obj = SomeMethod(); // what's the type? you'd have to inspect SomeMethod()
SomeClass obj = SomeMethod(); // the type is obvious
but other instances where using var makes perfect sense, e.g.
var obj = new SomeClass(); // the type is obvious
SomeClass obj = new SomeClass(); // the duplication of type is unnecessary
No, just readability.
In general no technical reason. Readability - in either direction - is the only real factor.
However, one small caveat is that var
will infer the static type of the variable. If you want a sub or super class type you'll need to do the casting yourself. In the case of a foreach
, as in your example, you can usually get downcasting performed for you "for free" just by declaring your loop variable with the subclass type.
The classic example is iterating over an XML NodeList that you know is a list of XmlElement
, but Nodelist
is typed as a collection of XmlNode
s. Of course you can use a cast or an as
to get back the type you want, but that would seem to defeat the purpose of using type inference :-)
Of course, the compiler will let you know about this as soon as you try to use a member of the node that is only available to XmlElement
- so it's still not strictly a technical difference.
Another thing that is a little annoying is that if you use a tool like Resharper, it's very aggressive about suggesting you use var
in every possible situation. It's particularly annoying when it recommends you change, for example, an int
declaration into a var
!
However, unless you turn that feature off, you'll get less "noise" from Resharper the more you use var
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With