When should you use "var" instead of "object"?

I was wondering when should you use var ?

Almost everything in C #, with the possible exception of primitives and a few more odd cases, comes from Object.

So, is it not better to use these actual types? or at least object ?

(I programmed for a while, and my strict point is that var is evil)

+4
source share
6 answers

You do not understand: var is not type. var instructs the compiler to use the correct type for the variable based on its initialization.

Example:

 var s = "hello"; // s is of type string var i = 42; // i is of type int var x = new [] { 43.3 }; // x is of type double[] var y = new Foo(); // y is of type Foo 

Your variables are still strongly typed when using var .

As a result, var not "evil." On the contrary, it’s very convenient and, as Ive said elsewhere , I use it extensively. Eric Lippert, one of the main people behind C #, has also described in detail whether var bad practice. In a nutshell, no.

+30
source

You combine var with dynamic , I think. Using var exactly the same * as if you wrote out the whole type name, and the compiler maps them to the same code. Eric Lippert's blog offers excellent information on what the var keyword does and when to use it.

From Eric Lippert's article Use and abuse of implicit typing:

To summarize, my advice is:

  • Use var when you need to; when you use anonymous types.
  • Use var when the declaration type is obvious from the initializer, especially if it is an object creation. This eliminates redundancy.
  • Consider using var if the code emphasizes the semantic "business" goals of the "variable" and downplays the "mechanical" details of its storage.
  • Use explicit types if necessary for the correct code to be understood and supported.
  • Use descriptive variable names regardless of whether you use "var". Variable names should represent the semantics of the variable, not the details of its storage; "decimalRate" is bad; "InterestRate" is good.

* Some may come across my “exactly the same” statement. This is not strictly true for anonymous types, but this is because you cannot legally spell a type name.

+9
source

Using object , you lose all information about the static type (and when using a value in a type, it even calls boxing). Therefore, using object very different than using var .

var , on the other hand, is equivalent to using the actual (static) type. So you use it when declaring the actual type is either impossible or inconvenient.

  • One scenario where var needed is to use anonymous types. They don’t have a name (nitpick: only the name generated by the compiler that you don’t know), so you cannot explicitly specify the type.

  • One case where using var convenient is when you are typename very long. Sometimes this happens when using nested generics.

  • And in foreach loops, you avoid unsafe implicit casts.

+5
source

var and object are two completely different things.

With var compiler infers the type for you. object is an object, and you lose any information about the instance, since C # is a strongly typed language.

See section 26.1 of the C # specification:

In an implicitly typed declaration of a local variable, the type of the declared local variable is inferred from the expression used to initialize the variable. When a local variable declaration indicates var as a type, and no type named var is specified in scope, the declaration is an implicit typed local variable declaration.

+4
source

Your "strict point of view" is a little misinformed ... var Uses the actual type. The compiler analyzes the return type of the expression on the right side and uses it as the destination type.

+2
source

var not a type. var tells the compiler: "I'm not sure / can't worry about entering the correct type name here, please make me a type based on the context of this statement."

Object superimposes an object on an Object type. As a result, you lose type information, so you can no longer call specific methods, access properties, etc., which are defined at a level lower in the hierarchy than Object .

+1
source

All Articles