Why does Resharper want you to change most variables to var type instead of the actual type in the code?
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'"
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.
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.
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".
var v = ...
:) –
Dday This is the explanation on the JetBrains code inspection wiki about it: http://confluence.jetbrains.net/display/ReSharper/Use+%27var%27+keyword+when+initializer+explicitly+declares+type
If you see the class to the right there is no big need to see it on the left as well. Also it saves space and reduces code if the class name is quite long. Personally I don't use var for simple types like string, int and so on but do use it for something like var dictionary = new Dictionary<string, int>()
to save space.
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.
SomeCode<int>
. –
Derian 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...
Vars help to make the code more readable inside a method, especially if you use generics.
As Jon says, it is just an option.
Status satus = foo.DoSomething()
. Writing this with var
hides the actual return type and makes it more unreadable. status
can be anything eg an int. –
Cousteau © 2022 - 2024 — McMap. All rights reserved.