tags:

views:

695

answers:

6
+3  Q: 

Resharper: vars

Why does Resharper want you to change most variables to var type instead of the actual type in the code?

+10  A: 

It's just an option. You can disable it:

ReSharper -> Options -> Code Inspection -> Inspection Severity -> Code Redundencies -> Use 'var' keyword where possible: change this to "Do not show"

There's also the context (lightbulb) option which will take you in each direction - this is under ReSharper -> Options -> Languages -> C# -> Context Actions -> "Replaces explicit type declaration with 'var'"

Jon Skeet
I guess from your knowledge of Resharper you think it is worth the licensing price?
CSharpAtl
Absolutely. I love it. Then again, for me as an MVP the licensing price was $0, so I'm biased ;)
Jon Skeet
Nice to know...thanks
CSharpAtl
The question is "why"?
Cherian
A: 

Vars help to make the code more readable inside a method, especially if you use generics.

As Jon says, it is just an option.

Jonas Kongslund
+1  A: 

To answer your question ... because someone at JetBrains decided that was the "preferred" way.

To change it follow Jon's answer. Additionally you can also change ReSharper's behavior when doing Code Cleanup (which I use a lot) under the Code Cleanup section in ReSharper options. Set to "Use Explicit Type".

Kyle West
This is prolly the only thing i dislike with Resharper, out of the box. IMO it promotes bad programming ... but var vs !var is a religous war .. so each to their own.
Pure.Krome
+4  A: 

By default, it will "green squiggle" declarations of this type :

Person p = new Person();
^^^^^^

Because of the repetition.

It will also suggest (small green underscore) var when it can be inferred :

Person p = repository.GetPerson(1);
¯¯¯

In this case it can be infered because of the return type of the GetPerson method.

As stated by Jon Skeet, you can disable these suggestions in resharper's options.

David Thibault
A: 

I think it suggests you both ways. If you have a explicit type - you can change it to var. If you have var - can change it to explicit. Just to make it faster for you to change if you think it is appropriate of course.

It might be good to use vars, for instance, for loop variables, when iterating a collection, so on, when the type is "implicit" for you (it is always implicit for compiler of course, when Resharper suggests it) and its absence doesn't make the code less readable. Also, I like it to shorten some declarations, which may grow quite long with generics. Like, IList(IDictionary(SomeType)) myVar = List(IDictionary(SomeType)) () would not loose much if you write "var" at the left side of the assignment.

(Replace parantheses with angle brackets ;)

Of course, I would try to use vars with care, to improve readability and not vice versa.

badbadboy
(you can use angle brackets if you contain within back quotes) `SomeCode<int>`.
mackenir
A: 

For me, it is definitely worth the price...(even if I had to pay it myself). But it can slow down your VS. It can get really slow if you have files like 5000 lines of code.

What I still don't get however is why I am the only one in the team using it...

badbadboy
Yeah, I'm looking forward to the next release, which is supposed to be performance-focussed. Also, me too :)
mackenir
A file with 5000 lines of code - wow. Sounds like some refactoring might be in order.BTw, you can split up the code file by defining partial classes.
Philipp Schmid
actually I have one with 6000. partial classes would not help - DNA :)
badbadboy