Good day!

Often I began to see expressions like var a = constructor or function in source codes

Why is var used? Why is the definition of a particular type of object not used?

And in the end, why var and not object if in Sharp any object inherits from object?

Ps it seems that var is already used both in place and out of place, like some answers on the forum using regular expressions, linq, and other scary words.

  • 2
    Var is the same as auto in c ++ 11 - PaulD
  • four
    @SoloMio: historically, var appeared earlier, so rather " auto is the same as var " - VladD
  • 2
    A powerful addition, @solomio, provided I don't know c ++ :-) - pincher1519

2 answers 2

Declaration

 var x = new List<int>(); 

strictly equivalent to a declaration

 List<int> x = new List<int>(); 

Just the compiler writes an obvious piece for you, so you don’t have to write the same thing twice.

Same declaration

 object x = new List<int>(); 

- a completely different beast! If in the previous case you could write x[0] , then in this case such code will not be compiled, since the object 's indexer (and all other functions specific to List<int> are not defined.


var has other advantages over explicit type declarations besides shrinking a record. The fact is that you can create a variable containing an object of an anonymous type , only using var (well, or using tricks with generics).


However, var cannot be used for field initializers.

  • Thanks, @vladd, i.e. it turns out var is not an object, it is a so-called alias of a piece of code that the compiler will complete. Understood. - pincher1519
  • one
    @uilenspiegel: Let me reformulate. 1. The compiler knows the type of the variable at compile time, because it knows the type of the expression on the right side of the initialization. 2. Whether the programmer knows the type of a variable depends on his mental abilities and knowledge of the standard. He can find out for himself the type of the right part, and he will be the type of the variable. 3. A declaration with var needed for anonymous types, as well as to avoid meaningless repetitions of code. - VladD
  • one
    @uilenspiegel: Yeah, I didn't quite clearly put it: he doesn't have to figure it out for himself (although he can, if he wants). Sometimes you can specifically write var in order to emphasize that the exact type is not important. - VladD
  • one
    @ pincher1519 csharpindepth.com/ViewNote.aspx?NoteID=61 - Costantino Rupert
  • one
    @ pincher1519 Know and write - different verbs. The fact is that all these C, pascals, javas are considered languages ​​with static typing and redundant type declarations. var is a way to simply not write too much. - alexlz

var is a keyword that allows you not to specify the type of a local variable in the case when it can be obtained from the right side of the expression.

It should be used everywhere where the type of the variable is obvious from the very right side of the expression, or where the type name is very long and clutters the code heavily.

  • I will take a turn, I will try as a local class, I might like it, although int i = 0; i ++; looks nicer than var i = 0; i ++; - pincher1519
  • one
    > int i = 0; i ++; looks nicer than var i = 0; i ++; It is a habit. Over time will pass. - nitrocaster
  • @flammable, by the way, what compiler will the object give in this case? Int, int, byte, longint, Int64 ... - pincher1519
  • @ pincher1519, int. - nitrocaster
  • one
    int , because this is the type of the right side (constant 0). ( System.Int32 is the same as int .) - VladD