Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any technical reason to use or not to use var in C# when the type is known?

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?

like image 631
Edward Tanguay Avatar asked Dec 15 '09 09:12

Edward Tanguay


People also ask

Is there ever a reason to use var?

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.

Is it good practice to use var?

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.

Is it necessary to use var keyword while declaring variable yes or no?

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).

Why you should always use the var keyword in C#?

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.


3 Answers

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
like image 116
Adam Ralph Avatar answered Oct 14 '22 07:10

Adam Ralph


No, just readability.

like image 28
Jorge Córdoba Avatar answered Oct 14 '22 07:10

Jorge Córdoba


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 XmlNodes. 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.

like image 21
philsquared Avatar answered Oct 14 '22 07:10

philsquared