C # vs R #: using var instead of explicitly specifying a type



    In my work with the remarkable ReSharper add- on in Visual Studio, I constantly came across a proposal to use implicit type declarations using var instead of explicitly declaring variable types . At first it surprised me a little, but I didn’t pay much attention. But after some time, such proposals began to strain and I decided to figure out what the essence of such optimization is.

    The answer was found in the blog of the creators of R # at this address . By katom my translation of the article by Ilya Ryzhenkov (Ilya Ryzhenkov).



    So, Ilya makes 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 here - you cannot define a variable of an anonymous type without using var;
    • using var forces you to more competently name the variables themselves. When you read the definition of a variable 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 take you more time 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 get optimal types when you let the compiler get the return type of the method or property itself. And also you will have to call your methods more correctly, so that they clearly indicate that they return;
    • using var forces variables to be initialized when they are declared. In general, the initialization of variables in the definition is a good form, and in our case, the compiler necessarily requires such initialization in the definition of a variable through var;
    • using var results in less "noise" in the code. There are many cases where implicitly declared variables reduce the amount of text that a developer has to read and which he might miss. If we do not use var, then defining the variable through the expression new or cast requires specifying the type 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 an iteration variable in foreach for a type like Dictionary <TKey, TValue> ;
    • using var allows you to reduce 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 another 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 redundant here.

    As the public thinks, are Ilya's arguments justified and, therefore, ReSharper's widespread use of var instead of explicitly specifying a type? For me personally, the arguments presented in the article seemed weighty and even correct. Who thinks?

    Also popular now: