
In my work with the wonderful addition of
ReSharper in
Visual Studio, I was constantly confronted with the proposal instead of explicitly declaring the type of variables to use type declarations implicitly using
var . At first I was somewhat surprised, but I did not particularly notice. But after a while, such proposals were already straining and I decided to figure out what the essence of such optimization was.
The answer was found in the blog of the creators of
R # at
this address . On the cut, my translation of the article by
Ilya Ryzhenkov (Ilya Ryzhenkov).

')
So, Ilya cites the following arguments in favor of the widespread use of “var”:
- You will need to use var to define a variable with an anonymous type. Everything is simple - you cannot define an anonymous type variable without using var;
- using var makes you call the variables themselves more correctly. When you read a variable definition with an explicit type, you get more information and something like “IUnitTestElement current” makes sense. However, when the local variable is used further, you will read “current” , which will require more time from you to understand what it means. Using “var currentElement” allows you to more quickly understand a variable anywhere in the code;
- using var forces a better API. First, you will get the optimal types when you allow the compiler to get the return type of the method or property itself. And you will have to call your methods more correctly so that they clearly indicate that they are returning;
- using var forces initialization of variables when they are declared. In general, initialization of variables when defining is a good form, and in our case, the compiler necessarily requires such initialization when defining a variable through var;
- using var results in less “noise” in the code. There are many cases in which variables declared implicitly reduce the amount of text that a developer has to read and which he could skip. If we do not use var, then defining a variable using a new or cast expression requires a type indication twice. When we deal with generics, this state of affairs will lead to the appearance of a large number of redundant code. Another similar example would be the foreach iteration variable for a type like “Dictionary <TKey, TValue>” ;
- using var reduces the use of the using directive. With var, you have no explicit type references, and since the compiler will determine the type for you, you do not need to import namespaces when you need some kind of temporary variable.
Here is an explanation. I would like to give one more comment on this article.
Alexander writes that
Microsoft does not recommend using var anywhere except in the case of anonymous types. To which Ilya answers simply: “Yeah, Microsoft often tries to make things„ safer “. I don’t agree with them here. ” I think the translation is superfluous here.
As the public thinks, are Illya’s arguments justified, and therefore ReSharper is making universal use of var instead of an explicit type indication? For me personally, the arguments presented in the article seemed weighty and even correct. Who thinks like?