

Data types:

Implicitly typed variables (the var keyword)

As of C# version 3.0, you can use the var keyword on the left side of a variable declaration, instead of explicitly declaring the type of the variable. This is only possible inside a method - not on the class level, where you always have to specify the type. Let's see how it looks when you use the var keyword:

int age = 42; // Explicitly typed variable

var name = "John Doe"; // Implicitly typed variable

Two variables - the one is declared with the int type, while the other one is declared with the var keyword, instead of specifying it as a string. Notice that I'm assigning a value in both cases, because while this is not required when declaring a type, it IS required when using the var keyword. The reason is that the C# compiler will infer the type from the right part of the statement - it simply looks at what you're trying to assign to the variable during the compile and then changes the var keyword into the appropriate type.

This also means that there is no overhead when using the var keyword - it's just as fast during runtime as an explicitly declared variable, because that's essentially what it is when the .NET framework executes your code.

Our first example is very trivial - there's not a whole lot of time saved in writing "var" instead of "string". However, sometimes you will be declaring much more complex types, either as a new variable or as the local result from a function (user-defined or from the framework). There's a chance to save a fair amount of keystrokes in an example like this one:

Dictionary<int, List<string>> dict1 = new Dictionary<int, List<string>>();

var dict2 = new Dictionary<int, List<string>>();

The result will be exactly the same, but you definitely save some keystrokes in the latter example, and since you specify the type on the right side in both cases, there's pretty much no loss in readability of your code when using the var keyword in an example like this.

You can also use the var keyword when declaring a local variable as the result of a method call:

var s = DateTime.Now.ToString();

Again, it's faster and it's still pretty clear what happens and which type the variable will hold (a string). That might not be in a case like this though:

var myVar = SomeMethodWithANameWhichDoesntIndicateTheReturnType();

In a situation like this, it's not clear at all what the variable will contain and you might be sacrificing the readability of your code. You might want to consider use an explicitly typed variable here.

The var Keyword & Anonymous Types

So far, the examples we have seen with the var keyword has been mostly from the "syntactical sugar" department - they are nice to have and shorter to type, but not really a requirement. However, when working with anonymous types (more on them later), it makes sense to declare your objects with the var keyword:

var myObj = new
    Name = "John Doe",
    Age = 42

In this case, you really need the var keyword to later access the fields of the anonymous type, as shown in this example.


The var keyword allows you to declare a local variable (inside a method or a loop) without explicitly stating the type - instead, the C# compiler infers the type from the right part of the declaration. This can be really convenient in a lot of situations, but it might also make your code slightly less readable. You can use it, or stick to explicitly declaring the variable types - whatever you prefer, but you need it for anonymous types, as already explained.
