0

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?

JBone
  • 3,163
  • 11
  • 36
  • 47
  • 1
    if you want to know more about var's there's a pretty explicit discussion here: http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c – rie819 Aug 31 '11 at 18:09

6 Answers6

7

The explicit variable type usage vs var is just a matter of preference. You can turn it off in resharper.

enter image description here

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Any opinions on why it would be someone's preference to change declarations to var after someone already declared them explicitly? – mbeckish Aug 31 '11 at 18:10
  • @mbeckish: ChaosPandion answers that question fairly well. http://stackoverflow.com/questions/7261390/c-initializing-variables/7261429#7261429 – StriplingWarrior Aug 31 '11 at 18:12
  • 1
    @StriplingWarrior - I agree with your answer and ChaosPandion, for the case where you are calling a constructor. I was more curious about JBone's first example: "int i = 0 ----> var i = 0". – mbeckish Aug 31 '11 at 18:16
  • @StriplingWarrior @ mbeckish some people do it for uniformity. For instance, for one of the projects I worked on, the coding standard enforced using var – Bala R Aug 31 '11 at 18:27
  • @Bala R - Did you find any benefits from the uniformity of always using var? Seems to me that a better uniformity rule would be something like "the type of the variable being declared should always be explicitly stated once in the statement." – mbeckish Sep 01 '11 at 13:21
5

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>>();
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • 1
    You have to type it once either way - type it first then hit the tab key a couple of times and it fills out the second half for you after the new. Either way you are only typing it once. – tsells Aug 31 '11 at 18:15
  • 1
    @tsells - That is true but consider the *Do Not Repeat Yourself* principle. – ChaosPandion Aug 31 '11 at 18:18
  • I would argue that this does not apply here. Especially with code assist functions in the browser. If you manually type it out that is one thing - but when using the tools as expected - DRY doesn't apply. – tsells Aug 31 '11 at 18:25
  • @tsells - No offense but that is a ridiculous assertion. The DRY principle is simple to understand and it is quite obvious when you are not following it. In fact I would say the principle stands well above any programming language or development environment. – ChaosPandion Aug 31 '11 at 20:31
  • @ChaosPandion A piece of advice - when you have to preface your statement with "No offense" you may want to rethink how useful it will be to the discussion at hand. Based on your theory every variable initialization in .Net prior to the introduction of the var keyword was violating the DRY principle. That is why I made the assertion I did - however ridiculous you think it maybe. It's been a pleasure....... – tsells Sep 01 '11 at 01:14
3

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.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
3

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.
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
2

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

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

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.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130