Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper: vars

Tags:

c#

.net

resharper

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

like image 955
CSharpAtl Avatar asked Nov 17 '08 20:11

CSharpAtl


3 Answers

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'"

like image 167
Jon Skeet Avatar answered Nov 11 '22 21:11

Jon Skeet


I saw a video from Hadi Hariri, where he was presenting Resharper 6.x. His reasoning was, if you are forcing a user to use "var", you are actually forcing him to name the variable in a more meaningful way, that way all the names are readable and make more sense.

like image 25
floatingmarbles Avatar answered Nov 11 '22 22:11

floatingmarbles


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.

like image 6
David Thibault Avatar answered Nov 11 '22 22:11

David Thibault