# Thursday, 13 May 2010

The var keyword was introduced in C# 3.0 and is used to declare a new variable.

The var keyword looks suspiciously like the variant keyword from Visual Basic 6. However, unlike variant, var is used to specify a strongly-typed variable. The type of the variable is inferred by the value initially assigned to that variable. As a result, whenever we use var to declare a variable, we must assign a value to the variable on the same line. So

var myString = "Hello";

is valid, but

var myString;

is not.

As mentioned earlier, the type of a variable is inferred from the value assigned to it, so in the line

var myString = "Hello";

is equivalent to

string myString = "Hello";


var myNum = 99;

is equivalent to

int myNum = 99;

The compiler is smart enough to recognize that "Hello" is a string and 99 is an integer and type the variable appropriately. This type checking is done at compile time, so the following code is invalid and will not compile

var x = 99;
x = "Hello";

because x is an integer, it is not possible to assign a string to it and the compiler knows this.

The var keyword is also smart enough to infer data types from the return types of functions. For example, if I have a function that returns an int and I assign the results of that function to a variable declared with the var keyword, that variable will be defined as an int. Here is a sample

static int AddNumbers(int x, int y)
return x + y;
var sum = AddNumbers(2,3);

The variable sum is an int because AddNumbers returns an int.

Earlier this year, following a user group presentation and someone noticed my use of the var keyword in my demos. The appropriateness of var became a Twitter debate and the subject of at least one blog post.

As long as I follow the rules, the compiler doesn't care whether I declare a variable using var or the explicit data type. So when do I use var? I use var when I think it will improve readability.

Consider the following 2 (equivalent) lines of code

Line 1: Customer cust = new Customer();
Line 2: var cust = new Customer();

Both lines are the same as far as the compiler is concerned. But the second line of code is more concise and easy to read. I have communicated the same information in a shorter command and with no loss of information. It should be obvious to anyone reading the second line that the variable cust contains a Customer object. In this case, I would declare the variable with the 'var' keyword because it is more readable.

The last example may not seem like there is much difference in readability between the two lines, but check out the following two equivalent lines

Line1: Dictionary<int, string> dict = new Dictionary<int, string>();
Line2: var dict = new Dictionary<int, string>();

Clearly, Line2 is more readable than Line1 in this case.

Now consider the following (equivalent) lines of code

Line 1: Customer cust = RetrievePrimaryCustomer();
Line 2: var cust = RetrievePrimaryCustomer();

Both lines are the same as far as the compiler is concerned. But I can't be sure, just from looking at the declaration, what data type is returned by the RetrievePrimaryCustomer method. The compiler knows the type, but I need to go through one or more manual steps to figure it out. In this case, the first option is more readable because it provides to anyone reading my code more information about the variable.

So my rule for using var is simple:

If the data type is obvious to anyone reading my code, use the var keyword; Otherwise explicitly specify the data type.

The var keyword is a useful shortcut that can help make your code more readable.