Implicitly Typed Local Variables
Implicitly typed local variables is a new compile time feature. Here is an example:
var implicitVariable = "Hello Implicitly Type Variable";
Debug.WriteLine(implicitVariable.GetType());
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";
Debug.WriteLine(oldVariable.GetType());
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.
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:
Post a Comment
Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.