Implicitly Typed Variables in C# 3.0

Posted Monday, April 30, 2007 11:50 AM by C-Dog's .NET Tip of the Day

One of the new features in C# 3.0 is the ability to implicitly type a variable based on what you assign to it using the var keyword. The compiler automatically determines the type by inferring it from what you assigned to it. You will see this a ton when you start to look at LINQ examples. I am not sure if I like this or not yet. Take a look at these examples.

var myInt = 4;
var myDouble = 9.0;
var myString = "blah";
var myDictionary = new Dictionary();

This is equivalent to the following traditional ways of declarion.

int myInt = 4;
double myDouble = 9.0;
string myString = "blah";
Dictionary myDictionary = new Dictionary();

So yeah its obvious what each variable is going to be based upon the value, but is this abbreviate of declartions a good thing or not.

Don't get this confused with the old ASP days where everything was a variant. Once a variable is assigned, it requires values for that type. For example the following would generate an error.

int x = 5;
x = "blah"; // error

I am curious to see what other people's thoughts are on this.

Read the complete post at