Wednesday, January 30, 2008

C# 3.0 - New Language Features - Part 2

Implicitly Typed Local Variables

Implicitly typed local variables is a new compile time feature. Here is an example:

var implicitVariable = "Hello Implicitly Type Variable";

The above code snippet's output will be "System.String"

When one looks at the code above - the obvious question that comes up is how is "var" different from using object. Because the above code could be re-written as follows:

object oldVariable = "Hello old variable declaration";

And the output would be exactly what the implicit type variable code would output "System.String".

So what is the point?

First of all - var - implicitly typed variables - are a compile time feature. Hence one needs to assign the var variable when its used. Thus the following are illegal statements:

var illegalStatement1;
var illegalStatement2 = null;

The reason that the above statements are illegal is that the compiler decides what the type of var variable is based on the assignment statement. In fact if you were to look at the IL code - using var or the actual data-type would result in the exact same code.

var implicitVariable = "Hello Implicitly Type Variable";
string oldVariable = "Hello old variable declaration";

Converts to

L_0008: ldstr "Hello Implicitly Type Variable"
L_000d: stloc.0
L_000e: ldstr "Hello old variable declaration"
L_0013: stloc.1

Another cool features connected with the var keyword? Because the implicitly typed variable is a compile time feature and the variable is assigned a type based on the assignment statement, one has full intellisense available for that variable.

Hence - in the following screen shot - intellisense for the string datatype members is available for the variable implicitVariable, but not for the variable oldVariable - which is of type object.

5 6

The place that I have found the var keyword most useful is when I need to enumerate over a collection. Often times one doesn't know what the type of the returned elements are when performing a foreach enumeration. By using the var keyword - one doesn't need to know the exact type of the returned objects and yet intellisense will be available for the objects - making it easy to discover methods and write code.

Dictionary<string, string> simpleDictionary = new Dictionary<string, string>();
simpleDictionary.Add("Hello", "A greeting in english");
simpleDictionary.Add("Raj", "Name of owner of this blog");
simpleDictionary.Add("brachypterous", "Having very short or rudimentary wings, as certain insects");

//old form of iteration
foreach (KeyValuePair<string, string> kyPair in simpleDictionary)
Debug.WriteLine(kyPair.Key + " " + kyPair.Value);
//new form of iteration using var keyword.
//use of var - makes intellisense available on the values of the enumeration
foreach (var dictionaryItem in simpleDictionary)
Debug.WriteLine(dictionaryItem.Key + " " + dictionaryItem.Value);




Intellisense available on the dictionaryItem as it was defined as a implicitly typed variable (var).


Finally, as this is a C# 3.0 feature - it can be used with modules that have been built against version 2.x of the .NET framework and is not isolated only to the 3.x versions of the framework.

The above is an example of a very simple use of the var type. People get most excited when it is used with LINQ - language integrated queries.

Next up - Automatic Properties.

No comments: