Good day.

I set myself here ReSharper. I didn’t really dig into the settings, but I noticed: he suggests MyClass MyClassEx all declarations of variables (eg Int32 SomeVar or MyClass MyClassEx ) to var... refuse to even use pseudonyms.

In this connection, the question arose: Are there any grounds (other than taste preferences) to use in certain situations certain methods for declaring variables? (are situations possible where different ways of declaring variables will be translated to IL in different ways?)

    2 answers 2

    Yes, there is a difference: if you explicitly define the type of a variable, it may differ from the type of the expression on the right-hand side. For non-virtual methods, this can lead to calling different code.

    Example:

     class A { public void Greeting() { Console.WriteLine("Hallo, I am A"); } } class B : A { public new void Greeting() { Console.WriteLine("Kekeke, I am B"); } } class Program { static void Main(string[] args) { A a1 = new B(); // заявленный тип переменной a1 - A a1.Greeting(); // выдаёт "Hallo, I am A" var a2 = new B();// заявленный тип переменной a2 - B a2.Greeting(); // выдаёт "Kekeke, I am B" } } 

    The same effect with open fields (they are always non-virtual) and non-virtual properties.


    There are cases when you have to specify an explicit type (or make upcast) manually. Example:

     class C : IDisposable { void IDisposable.Dispose() { } } 

    This code does not compile:

     var c = new C(); c.Dispose(); // нет такого метода 

    And this one compiles:

     IDisposable c = new C(); c.Dispose(); 

    In the case when the explicitly defined type coincides with the type of expression, there are no differences.


    I think Richter is too strict. In 2016, the exact type has long been not so important. Semantics, the meaning of variables is much more important. Therefore, I use the rule for myself:

    • Wherever possible (that is, almost everywhere), prefer type aliases ( int , not System.Int32 ).
      • Exception: if this is important and I want to emphasize that my type contains exactly 32 bits
    • Where the exact type of the variable is really important or I want to emphasize it, use it, otherwise use var . For example, if an operation returns me a Stream or NetworkStream there, I usually don’t have a specific type, and I’m just using var .
    • 2
      Already all painted, nothing special to add. But still, I would note one important thing: if when replacing var with a specific type or vice versa, the code breaks, then it is worth considering whether it is good code. Accordingly, the choice between var and a specific type is the holivar of the placement of curly bracelets, moreover, adherents of different languages ​​have different traditions. - Athari
    • @Discord: very correct, I agree. - VladD

    There are situations where the use of var necessary. For example, the declaration of anonymous types, especially when using LINQ. Example:

     var some = new { Id = 10, Name = "qwerty" }; 

    The type is created automatically, and only the compiler knows its name, and therefore it is impossible to do without var here. The IL-code will be generated the same as for the types "hiding" under the var , and for the explicitly declared. var is just syntactic sugar. In general, following or ignoring this recommendation of Resharper is by and large a matter of taste. Personally, I prefer var in obvious situations, where the type is uniquely determined from the right side. For example, when using constructors:

     var some = new MyClass(); 

    In this case, the use of var justified in order to avoid duplication of the class name (as a bonus, if you want to change the type of some to some other variable, you will have to edit it one time less, although these are trifles).

    However, it is better not to use var , if it is impossible to unambiguously determine from the right part of the expression what type it is. For example:

     var some = GetData(); // тип переменной some не очевиден 

    in this case, it is better to explicitly specify the type:

     MyClass some = GetData(); 

    From my personal preferences: I do not specify var when using primitives. Instead

     var val = 0.0; 

    I prefer to write

     double val = 0.0; 

    Although this can be attributed to special cases, the rules on the type of evidence from the right side.