Nov 05

The var keyword

Posted in Uncategorized      Comments Off on The var keyword

In these last days, I”ve been looking at LINQ. I”m still not sure if this will help me build applications easily or if the new features introduced on the C# language are making it a lot more complex than it should be (when I make up my mind, I”ll let you know :),,). Anyway, LINQ brings us some cool things and that”s why I”m taking a look at it. before delving into LINQ, we should understand the new features of the C# language that support it. Today I”m going to write about the var keyword (there are already several posts on this, so I”m only writing this to use as a future reference).

Basically, the var keyword was introduced to let you declare a variable without defining its type explicitly. The C# 3.0 specification states that this type of declaration is known as implicitly typed local variables. Lets take a look at a quick example:

var myInt = 10;

If you run the previous sample, you should see System.Int32. So, what”s happening here? Well, when you build your app, the C# compiler will infer the type of the variable from the initialization expression you”re written. It”s really important to keep this in mind: the type of the variable depends on how the initialization expression is parsed by the compiler. Here”s another example:

var myInt = 10.4;

What do you expect to see when you run this program? will you get a Single (float) or a Double (double)? You”ll get a double because that”s what the literal 10.4 is. If you want to get a single you need to use the f prefix (var myInt = 10.4f;).

From these examples you can already deduce that you cannot use the var keyword without an initialization expression (if you do that, how will the compiler infer the type of the variable?). Other things to keep in mind are (taken from the C# specs):

  • the initializer must be an expression and cannot be an object or collection initializer;
  • cannot use the null value;
  • if the initialization expression contains multiple declarators, they must all be of the same type.

Lets look at same examples:

var myInt = null;

This won”t compile because the compiler cannot infer the type of a variable from null.

var myIntArray = {1, 2,3 };

This goes against point 1 of the previous list. you need to indicate the type of array like this:

var myIntArray = new Int32[] {1, 2,3 };

Not really that complicated, right?

A couple more of observations:

  • I”ve been saying that var is keyword, but it”s nor really a keyword in the traditional sense (when you think about keywords, you normally think about terms with predefined meaning that cannot be reused in a program – for instance, you can not create a variable named class). You can, for instance, introduce a new class called var . If you do that, when you write var myVariable you”re really creating a new variable of type var  (not an implicitly typed local variable);
  • you can use var in foreach cicles and using instructions since the compiler will be able to infer the type of the variable you”re creating.

Well, as you can see from this short presentation, implicitly typed local variables are simple and they really depend on the compiler (not on the CLR). I still haven”t used this new feature on my C# 3.0 projects. But again, I still haven”t written any LINQ aware application…