C# 3.0 for mere mortals, part 2: Implicit typing

Forget all you know about variables that are declared “var”! You probable know about var in JavaScript. Yes, you should forget that too.

Microsoft took quite a chance by choosing the “var” keyword in C#. var has nothing to do with previous uses of the keyword. So, what is it for then? The C# 3.0 compiler can now deduce the type of variable you need, like so:

var lazy = “Dude”;
var complexType = new LinkedListNode<bool>();

Here lazy would be of type string and complexType of type LinkedListNode<bool>. The type of your variable is defined implicitly. That is sometimes convenient, because you wouldn’t need to type the exact type (or twice). But that is not what is was really meant for. var is meant for moments when you wouldn’t know the exact type. You might think: “Hey, that’s why we have the object keyword, because every type is ultimately derived from System.Object”. Wrong. Variables declared with var are absolutely something different than variables declared of type object or System.Object. Note that I did not say “variables declared of type var”. That’s because var is substituted by the compiler by the actual type that the compiler has infered/deduced.

Would you have used object instead of var to declare a variable, the compiler would not know of the actual type that was referenced through this variable. You would notice so, becuase the IDE of Visual Studio would not give you IntelliSense information. Only after casting does the compiler and IDE know enough your variable’s type to provide IntelliSense. Referencing a value type would lead to boxing of the value, like always.

With a var variable none of the above is true. The IDE will know about your type, because it has determined the type itself from an “indirect definition” or value. No boxing needed. And IntelliSense (at least in a future version, because the PDC preview for the RTM version of VS2005 does not have it yet).

Let’s move on a bit. How can it be that “you wouldn’t know the exact type“. That has to do with the new feature in C# 3.0 called “anonymous types”. The compiler will sometimes generate new types for you during compilation.

var typeWithNoRealName = new { Name=”Alex”, Age=34 };

These classes do not have an explicit name, like “normal” types do. We’ll cover anonymous types in more detail in a later post.

Anyway, we need a way to declare a variable of a type that is determined at compile time. That’s exactly where var comes in. The circle is round.

The var type even extends to array types.

var names = new[] { “Nicole”, “Lieke”, “Alex” };
var flags = new[] { true, false, false. true};

Here, names will be a string[] and flags a bool[].

There are some rules you need to play by, as always.

  • The compiler must be able to deduce the actual type of the initialization value.
    You must have an initializer (assignment during declaration) and it cannot be a null reference or collection initializer.
  • In array initializers the types must be equal or at least convertible from one to the other.
    var names = new[] { “Sweet”, true, Color.Red }; // won’t compile

The use of var will be clearer once we get to the anonymous types. Those are up next.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s