[C# 3] var, Object Initializer, and Anonymous Types

If you have ever used JavaScript, you know the “var” keyword is used to declare variables no matter what type of variable you create. Even you can assign any type of data to the same variable at any time.

In C#, all variables should be strongly typed, which means that the data type of a varaible should be defined at compile time and cannot be changed to another type. Therefore the JavaScript’s “var” type of variable was not necessary.

But something happened on C# 3.        

1. Object Initializer Syntax

C# 3 offeres a new way to create an object and initize the properties.

In C# 1 and C# 2, you need to provide constructors to initialize the internal variables. In C# 3, you can initialize an object like this.

public class Point2D
  public int X { get; set; }
  public int Y { get; set; }

  public Point2D() { ... }
  public Point2D(int x, int y) { ... }

Point2D p = new Point2D(10, 20);

Point2D p1 = new Point2D { X=10, Y=20 }; // default constructor is called implicitly
Point2D p2 = new Point2D() { X=10, Y=20 }; // default constructor is called explicitly
Point2D p2 = new Point2D(1,2) { X=10, Y=20 }; // custom constructor is called explicitly

You might say what’s the point of this? Is it even useful? I agree with you.  In general, it is not useful at all. Moreover I recommend using constructors.

The purpose of this syntax shines when it is used for anonymous types.

2. Anonymous Types

New C# 3 features are more or less realted with LINQ.

From a LINQ query, you can project output in many ways using the “Select” operator. The problem is that you need to define a class for each output projection. This behavior might be required in most cases, but is sometimes quite cumbersome expecially if you need temporary data set.

Anonymous types are the solution for this. You can create an object without a class definition. The compiler creates IL for the class for you.

It is easy to create an anonymous class. Use the “new” keyword and the object initializer syntax.

new { X=10, Y=20 };

That’s all. The compiler will choose a name for the class and create 2 properties X and Y for you. Now you understand why the object initializer syntax is added to C# 3.

But we are not done yet. How can we access this object. You do not know the class name and there’s no way to create a variable.

3. Implicitly Typed Variables

C# 3 had to create a new way to assign a reference to the instance of an anonymous class. You do not know the name of an anonymous class but the compiler knows it.

The “var” keyword is used to tell the compiler to infer the type during compilation.

var p = new { X=10, Y=20 };

The compiler will create a class and replace “var” with the new type.

The “var” does not need to be used only for anonymous types. The compiler always infers the type for you.

var i = 10;
var s = "Hello";

Both will work. But I strongly recommend that you do not use “var” if you can figure out the type. The use of “var” should be limited with anonymous types. (Sometimes you can use “var” when the output of a LINQ query is so complex and it is hard to figure out the output type.)

4. Restictions on Implicitly Typed Variables

  • Should be applied only to local variables in a method or property scope
  • Cannot be used to define a return type of a method
  • Cannot be used to define a parameter type of a method
  • Cannot be applied to an instance variable
  • Must be assigned a value at the time of declaration

5. “var” is Strongly Typed

C#’s “var” is different from that of JavaScript. The “var” variable is still strongly typed, which means that the type should be determined at the compile time and you cannot change the type.

The following code works in JavaScript but fails in C#.

var i = 10;
i = "Hello";

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