Wednesday, December 21, 2011

Type inference/Implicitly Typed local variables

One of the new features in C# 3.0 is Implicitly Typed local variables. In scripting languages like JavaScript, you can define a variable like this -

var x = “some value”;

You can now do the same thing with C# 3.0 and although some people may consider this feature to be nothing more than syntactic sugar or a declaration shortcut for lazy programmers, it is needed for declaring anonymous types and while using LINQ (which I’ll cover in future posts).

C# uses type inference to automatically detect the type specified on the right hand side of the declaration. So, all these declarations are valid:

var myName = “Mahesh Krishnan”;
var myHeightInCms = 182;
var myWeightInKgs = 76.432;
var myPet = new Cat();
var dict = new Dictionary();

var b = (ans == “Y“) ? true : false;

The main rule is that the variable has to be declared in one statement as shown above. Under the covers, C# substitutes var with a string or an int or whatever type is specified on the right hand side of the declaration. This is done at compile time and as a result type safety is not compromised. When you use Visual Studio 2008, you will also notice that intellisense works correctly for the type used.

You cannot have an empty or null declaration

The C# compiler needs to know the type of variable it is creating. So an empty variable declaration or a variable declaration that is assigned to null is not allowed.

//Neither statements are valid
//var uninitalized;

//var x = null;

Once a variable has been assigned, you cannot re-assign it to another type

In languages like JavaScript, you can assign an object to a variable and then re-assign another object of a different type to it. But in C#, this is not legal.

var myBooleanValue = true;

//Cannot change types!
//myBooleanValue = “false”;

You cannot mix and match types

//Not allowed
var x = (myBooleanValue) ? true : “false”;

As I mentioned earlier, type inference is done during compile time and a statement as shown above is not supported as the type of x can only be determined at run time. As a result, this statement is not valid.

You cannot create an array of vars

Creating an array of var as shown below is not allowed:

//Not valid
//var[] arr = new int[] { 10, 20, 30 };

You can still assign an array to an array variable declared as a var as shown below -

var arr = new int[] { 10, 20, 30 };

I will cover implicitly typed arrays in a later post.

Can only be used for local variables

One other major rule is that you cannot use var to declare member variables in a class. They are only allowed within a function body.

Use them wisely

Although, using var to declare a variable is a nice feature, use it wisely. Using it to declare known types such as integers and strings can make the code a bit unreadable. Another thing to avoid is using the keyword var as a variable. This is still allowed for backward compatibility, but you should avoid it at all costs.

No comments: