Reputation: 3203
I am programming c# in VS2010 and have been testing out a plugin called ReSharper. In my experience I feel it cleans up the code and simplifies unnecessary logic. However one of the main problems it picks up all over my code is when I am initializing variables.
Resparper recommends initializing wit "var"
int i = 0 ----> var i = 0
or...
MyClass1 MyClass = new MyClass(1)
I always thought that using var had a slight overhead and seems kind out lazy or loosely types (although I do realize it has some advantages). I have been having trouble trying to find if there is a good reason to initialize variables like this. Reshaper says that using var is a controversial issue but it has good augments to use for initialization. Does anyone know what this may or may not be?
Upvotes: 0
Views: 836
Reputation: 39109
var
is not a type in itself. It just orders the compiler to look up the type on the right side of initialization, which is why the following won't compile:
var x; // invalid because x is still statically typed
So, neither will it have a bad effect on compilation time, nor does it have any runtime overhead. It is basically syntactic sugar, the generated "machine" code will be the same.
Upvotes: 1
Reputation: 156624
var
has no runtime overhead and fairly minimal compile-time overhead. The IL code it produces is actually equivalent to if you use an explicit type. Whether you prefer to use it or not is entirely a matter of preference. I've personally set Resharper to not bother me unless the initialization makes the type obvious:
Person p = new Person(); // Resharper bugs me
int i = 0; // Resharper leaves this alone.
Upvotes: 3
Reputation: 44605
the point is that when you assign a variable with a direct usage of new Classname(...)
you clearly absolutely and uniquely know the final type of the variable, because just called the constructor.
In this case var has a nice syntax and makes things compact, with the advantage that if in the future you will change the assignment with another type of object, changes are minimal.
in cases where it's not absolutely clear the type of object assigned, I still like to make it explicit.
but in the end is almost a philosophical issue :D
Upvotes: 2
Reputation: 78282
If this is lazy then I don't want to be productive.
Dictionary<int, Dictionary<string, string>> map =
new Dictionary<int, Dictionary<string, string>>();
var map = new Dictionary<int, Dictionary<string, string>>();
Upvotes: 5
Reputation: 160962
using var
is just using implicit typing instead of explicitly calling out the type, a variable declared with var
is still strongly typed and will be compiled down to the same code as the explicitly typed variable - no overhead.
The only times there is a difference is when you want to have the variably typed differently than the expression it is assigned to, i.e. you want the variable to have the type of an interface when the expression returns a concrete class that implements that interface. In these cases you should declare the variable explicitly.
Upvotes: 3
Reputation: 108977
The explicit variable type usage vs var is just a matter of preference. You can turn it off in resharper.
Upvotes: 7