Possible Duplicate:
C# 'var' vs specific type performance
Hi all,
I recently saw code that uses var a lot.
E.g.:
var myString = GetAnyString();
var myInstance = GetClass();
instead of
string myString = GetAnyString();
MyClass myInstance = GetClass();
Besides the readability (I think that using var is not very readable), are there any advantages and/or drawbacks using it? How about performance
var is replaced by the compiler with the right type, so there is no runtime performance hit, aside from a very small compile time overhead maybe (veeeeeeeery small).
Actually, from a readability point of vue, I personally do as follows:
Variable instantiation:
object myVar = new object();Variable propagation:
var myVar = anotherVar.getComplexObject();Also, you might be tempted to use them when you have a very complex type instantiation, like:
Here, it really improves readability.