In my previous blog we had a look at Automatic properties in C# 3.0. In this blog we will try to understand what implicitly typed variables are? To know more read on.
What is Implicitly Typed Variables?
In our day to day C# programming we declare variables something like this.
int i = 6;
We have to first specify what is the data type and then the initial value for the variable. With C# 3.0 you need not specify the data type, instead you can make use of the “var” keyword. The above code can be rewritten as shown below.
var i = 6;
Using the “var” keyword will tell the C# compiler to infer the data type of the variable from the initial value supplied. One thing to note here is that if you don’t provide an initial value the system will throw the following error.
|“Implicitly-typed local variables must be initialized”|
It’s mandatory to use an initial value; this is because the system infers the type of the variable based on the initial value assigned. Some of you may argue that instead of int and string we are using var and there is not much of a difference. I do agree, as implicitly typed variables were brought to support anonymous types (we will see anonymous types in a future blog) and they can very well be used to write terse code, another use of implicitly typed variable.One can really see the advantages of using var when one uses generics. Suppose you want to create a generic dictionary to hold two of your custom objects like User and his Address, a typical code to declare the dictionary would look something like this.
|Dictionary<User, Address> userAddress = new Dictionary<User, Address>();|
One can make out from the above code that there is a lot of repetition in the form of “Dictionary<User, Address>”. With implicitly typed variable we can shorten our code as shown below.
|var userAddress = new Dictionary<User, Address>();|
The User and Address classes are shown below. The properties are declared using Automatic Properties.
The User and Address class objects can be initialized using the var keyword as shown below.
var user = new User();
One can use the var keyword to declare variables for any class. Few e.g. are shown below.
var dataset = new System.Data.DataSet();
Implicitly typed variables can be used with “for” and “foreach” loop, with “using” statement and for collection initialization. The following e.g. show each in action.
//Collection intialization using implicitly typed variable.
//Implicitly typed variable used in a for loop.
//Implicitly typed variable used in foreach loop.
//Implicitly typed variable used along with using statement.
As I said earlier, many of will say using “var” or implicitly typed variables do nothing more than saving a few lines of code but in realty implicitly typed variables were introduced in C# 3.0 to support Anonymous types. As the name suggests anonymous types don’t have any type. We will have a detailed look at Anonymous types in my next blog. Just to give a brief idea on anonymous types see the below LINQ code.
//Stupid code to fill the an array with User objects
The above code makes use of LINQ query to filter out User’ whose country equals “India” and creates an anonymous object collection with FirstName, LastName and Email as their public property. The “where” extension method in LINQ filters the user’ with country as India and the “select” extension method creates the anonymous type with properties FirstName, LastName and Email. If one wants to make use of the anonymous collection type, indian, then he/she has to use the var keyword in the loop as shown below.
// foreach statement making use of the anonymous collection.
var i = 6;
Some points to note
- var keyword was introduced to support anonymous types and overuse of var in your code can make your code terse but hard to understand.
- var can only be used on local variables i.e. methods declared inside a method.
- var cannot be used to initialize a variable to null.
- var can be used in “for” loop, “foreach” loop and in “using” statement as shown in e.gs above.
- Class level variables cannot be decalred using the var keyword.
In the next blog we will have a look at Object Initializers and Collection Initializers, till then try to know more.