Wednesday, November 28, 2007

Implicitly Typed Local Variables: C# 3.0

Remember var's - well if you are a native VB or a COM or even an OO COBOL developer you will have almost certainly used variants/objects. Var is a new keyword used in C# 3.0, but don't mistake it for a variant or a typeless object - it is far from it.

The var keyword allows you to define a variable whose type is implicitly inferred from the expression used to initialize the variable. The var keyword is basically a compiler trick, there is no difference in the generated IL when implicitly delcaring the var from explicitly declaring the integer. So you could code the following in C# 3.0 which is perfectly legal:
var i = 0;
After executing the above code the CLR sets the type to an integer. i is strongly typed, and treated the same as defining an integer variable. So the above is the same as the following:
int i = 0;
As you can see the var has been inferred to an integer value type:

One requirement is that the var is initialized on the same line as the var is declared and the intializer must be an expression. So you couldn't code:
var i;
Trying to compile the above code will give a compiler error: "Implicitly-typed local variables must be initialized":

You also couldn't set i to null attempting to do this will generate compiler error "Cannot assign to an implicitly-typed local variable".

So the following is invalid:
var i = null;
You also must define the var in-line. You cannot define the var outside of the member, event, property etc attempting to do this will generate compiler error "The contextual keyword 'var' may only appear within a local variable declaration":

So we have covered the basics of implicitly typed local variables and I bet you're probably thinking, what is the point of them, I mean doesn't this just make you're code more difficult to read?

Well the point of them is they go hand-in hand with LINQ and Anonymous Types. LINQ is a set of extensions to the .NET framework 3.5 that encompass Language Integrated Query set and transform operations. LINQ is available with C# 3.0 and Visual Basic 9. You will need the C# 3.0 or VB 9 compiler and .NET Framework 3.5 to use LINQ which come with Visual Studio 2008. In addition, var is generally used with Anonymous Types.

I have written an article on Anonymous Types, if you haven't read this yet, you can do so here.
I will be writing some articles on LINQ soon...As LINQ is very powerful and will make our jobs as developers easier. I can see LINQ simplifying the data layer completely. I've never liked the complexity of ADO.NET - it's too problematic which is why I have been using the enterprise library data application blocks from the pattern & practices group for ages now. LINQ is going to make data access even easier and more importantly, cleaner.

But .. I will talk more about LINQ to come. There is a lot of information on LINQ in the communities - which is great, the sooner everyone starts using it the better!

No comments: