views:

440

answers:

10

I've just started using ReSharper with Visual Studio (after the many recommendations on SO). To try it out I opened up a recent ASP.NET MVC project. One of the first and most frequent things I've noticed it suggesting is to change most/all my explicit declarations to var instead. For example:

//From This:
MyObject foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
//To This:
var foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);

and so on, even with simple types such as int, bool, etc.

Why is this being recommended? I don't come from a computer science or .NET background, having "fallen into" .NET development recently, so I'd really like to understand what's going on and whether it's of benefit or not.

+16  A: 

One reason is improved readability. Which is better?

Dictionary<int, MyLongNamedObject> dictionary = new Dictionary<int, MyLongNamedObject>();

or

var dictionary = new Dictionary<int, MyLongNamedObject>();
Nebakanezer
I would say the first one. Easier to see whats going on!
Mongus Pong
SO's scrollbar is making a point here I guess ;)
Bryan Menard
Fungus: Do you like Do you like Redundant Text Redundant Text? :D
Mark Simpson
@Zxpro: Thanks for pointing that out, gave me a chuckle.
Mayo
Thanks. Consensus seems to indicate it's just a readability suggestion, rather than being of benefit at compile time.
Chris
@Chris, exactly that. It makes no difference at compile/runtime, it's there for readability and (primarily) for supporting anonymous types, I believe. =)
Rob
@Fungus: add also a comment to make it absolutely clear: declaring the 'dictionary' variable of type 'Dictionary<int, MyLongNamedObject>':)
Kamarey
A: 

There is no technical difference, if you use var, the type is implied by the compiler. If you have a code like this:

var x = 1;

x is implied to be an int and no other values can be assigned to it.

The var keyword is useful if you change the type of the variable; you then only have to make one change instead of two:

var x = 1; --> var x = "hello";
int x = 1; --> string x = "hello";
eWolf
+11  A: 

I personally prefer to turn this suggestion off. Using var can often improve readability; but as you mentioned, it sometimes reduces it (with simple types, or when the resulting type is obscure).

I prefer to choose when I use var and when I don't. But again, that's just me.

Bryan Menard
I thought ReSharper was meant to be pretty smart; Shouldn't it be smart enough to know when the resulting type is obvious (e.g. anything with the new keyword) and when it is not obvious?
DisgruntledGoat
Well, I don't know the peculiarities of the feature but, I sure know I was overwhelmed by the amount of suggestions it gave; And I use `var` fairly often too.
Bryan Menard
+4  A: 

I disliked this as well.

I dont want this to turn into a debat on the use of Var, it has its use's but should not be used everywhere.

The key thing to remember is re-shaper is configured to what ever coding standards you want.

Edit: http://stackoverflow.com/questions/737835/resharper-and-var

Pino
+4  A: 

ReSharper recommends var because it tends to unclutter object creation.

Compare these two examples:

StringBuilder bld = new StringBuilder();

var bld = new StringBuilder();

It's just a shorthand that is supposed to be easier to read.

i think it's fine when you create new objects explicitly with "new". In your example however, it might not be obvious if the classes were not named properly.

Paul Sasik
+3  A: 

The var feature of .Net 3.0 is just type inference, which is type safe and often makes your code easier to read. But you don't have to, and can turn that recommendation off in resharper if you want.

klausbyskov
+8  A: 

What ReSharper suggests is clearly overuse of the var keyword. You can use it where the type is obvious:

var obj = new SomeObject();

If the type is not obvious, you should rather write it out:

SomeObject obj = DB.SomeClass.GetObject(42);
Guffa
To play devils advocate, maybe if the type is not clear from the method or the variable name, it indicates a problem with naming more then an overuse of var. I do agree in principal though, var should only be used when it is not removing clarity.
Matt Briggs
In this instance I would rather use better variable names. You are basically proposing that we look up to see where the variable is defined to figure out the type - I am proposing that we name the variables better so that we know the purpose of the variable offhand.
Jaco Pretorius
@Jaco: +1, but it's worth to mention that information about type is not recommended to be in a variable name. For example, Hungarian notation is not considered to be a good practice.
Roman Boiko
Whether ReSharper's default settings are an overuse of `var` is a matter of opinion, and not "clearly" one thing or another. I prefer not to type things that the compiler can figure out for itself. I like C# type inference, and often wish it was as good as F# type inference. If I could, I'd leave out explicit types from method parameters and return types, as is the norm in F#. Not everyone agrees, of course.
Joel Mueller
+2  A: 

BTW, ReSharper draws a distinction between 'you might want to apply this suggestion to your code' and 'your code is broken, want me to fix it?'. The var keyword is in the suggestion category, along with things like "invert if to reduce nesting"; you don't have to follow it.

You can configure how annoying each of its alerts are through the Options dialog, or directly though the popup menu for that alert. You can downgrade things like the var suggestion so they're less prominent, or you can upgrade things like the 'use extension method' alert so it shows up as an actual error.

Tim Robinson
+1  A: 

The var keyword was introduced in C# 3.0 - it allows us to forget about specifying our type explicitly.

There is no real difference to whether you use

MyObject foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);

or

var foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);

except pure readability and less chance for error.

It seems like a clichéd example, but say the following may help your understanding:

var myInt = 23;

returns an int type, whereas

var myInt = "23";

returns a string type.

MSDN reference

Daniel May
A: 

There is no technical difference (as eWolf pointed out). You can use one or the other, the generated CLR code will look the same.

In my opinion the main benefit is that this tends to force you to use better variable naming. In your example 'foo' is a pretty poor choice for a variable name.

Jaco Pretorius