BlackWasp
C# Programming
.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 developing software using the .NET framework version 2.0 or earlier, all variables must be declared explicitly. This means that each new variable is declared with a specific data type as its prefix. For example, the following line of code creates a new string.

string fruit = "Apple";

In C# version 3.0, Microsoft introduced the concept of implicitly typed local variables. This provides a new declaration syntax that instructs the compiler to infer the type of a new variable according to the value assigned to it or its initial usage. To use implicit typing, a variable is declared using the "var" keyword. The previous sample can therefore be rewritten as:

var fruit = "Apple";

When this statement is compiled, the compiler detects that a string value is being assigned and infers that the new variable should also be a string.

Strong Typing

When programmers see this new syntax for the first time, it is not unusual that they incorrectly 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 sample shows some further implicit declarations. You can see that the assigned value determines that 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 implicitly typed variables 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 that have been 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 accordingly.

var x;
var x, y;
var x = null;

Usage Recommendations

At first glance, implicitly typed variables appear to be simply syntactic sugar. In some cases they become a distraction and the readability of code is decreased with their use. 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 each of 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.

Link to this Page2 June 2008
RSS RSS Feed