.NET 3.0+C# Implicitly Typed Variables
C# 3.0 introduced the concept of implicitly typed local variables. These are compiled in exactly the same manner as their explicitly typed counterparts, except that the compiler automatically determines the variable type according to its usage.
Explicit and Implicit Typing
When using the .NET framework version 2.0 or earlier, all variable types must be declared explicitly. Each new variable is declared with a specific data type as its prefix. For example, the following creates a new string.
C# 3.0 introduced the implicitly typed local variables. These provide a new declaration syntax that instructs the compiler to infer the type of a new variable according to its initial usage. To use implicit typing, a variable is declared using the "var" keyword. The previous sample can be rewritten as:
When this statement is compiled, the compiler detects that a string value is being assigned and infers that the new variable should be a string.
Strong Typing
When you first encounter this new syntax, you may assume that "var" is a new data type, perhaps similar to the variant type of Visual Basic 6 or to the existing object class. In fact, variables created in this manner follow all of the rules of the strongly typed C# language. Once compiled, the resultant code is indistinguishable from that of an explicitly declared variable. This can be demonstrated by attempting to incorrectly assign an incompatible value:
var fruit = "Apple"; // String variable
fruit = 5; // Causes compiler error
The following shows some further implicit declarations. You can see that the assigned value determines the data type selection.
var igr = 1; // int
var dbl = 1.1; // double
var dec = 1.1M; // decimal
var stg = "Hello, world!"; // string
var dst = new DataSet(); // System.Data.DataSet
for (var i=0;i<10;i++) // int
{...}
Limitations
There are limitations to the use of implicitly typed variables. One of the most important is that they may only be used locally within class members, such as methods and properties. They may not be used in any element of a class where the variable could be a part of the public interface. This prevents their use as class-scope fields, properties or the return type or parameter types for a method. Variables initially declared using "var" may, however, be passed to the parameter of a method, assuming that the types are compatible.
As seen above, the initial declaration or usage of a variable determines its type. This means that the compiler cannot infer the type of a variable that is not assigned a value or declared using the "new" keyword. The following three declarations cause compiler errors.
var x;
var x, y;
var x = null;
Usage Recommendations
At first glance, implicitly typed variables appear to be syntactic sugar. Sometimes they can be a distraction and the readability of code can be decreased. However, in some scenarios it is essential to declare variables in this way. Two particularly important reasons for using "var" are when using anonymous types and language-integrated query (LINQ). In these cases, the type of variable required may not be known or may not even exist.
As a general rule, the "var" syntax should be used with care. This is particularly the case where readability or maintainability of code may be decreased by its use.
2 June 2008