“VAR” keyword in .NET and c#

Its a implicit or indirect way of defining data types i.e by looking the data at RHS the LHS data type are define by the compiler during the generation of the IL Code. In IL code actual data type will be present as per data.

Var keyword define data type statically and not on runtime i.e value type can not be change at runtime.

When to use VAR ?

  • In Linq query where we are not sure what kind of columns query will return i.e Anonymous Type. If we will not use var then we have to create a POCO class to store the Linq query result which will be a overhead.
  • Rarely but when class name are big enough to write


var i= 10;

i = "Jogendra"; //Here it will give the COMPILE time error which proves that var keyword data type set statically

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s