jump to navigation

Object and Collection Initialisers in C# 3.0 June 30, 2008

Posted by fofo in .NET, C#, Visual Studio 2008.

This is a just another post which discusses enhancements in the c# language.

In this post I will talk about object initialisers.

To demonstrate this new feature we will create a new asp.net web application

1) Add a new item to your application. A class (.cs file). Name it Thecar.cs.

2) The code inside this class is following. I just implement some public properties and I leave the constructor as it is.

public class TheCar

public string Make { get; set; }
public string Model { get; set; }
public DateTime orderdate { get; set; }
public int milesofar { get; set; }

 public TheCar()
  // TODO: Add constructor logic here


3) In the Page Load event of the default.aspx page add the following code

TheCar myCar = new TheCar();

myCar.orderdate = DateTime.Parse(“12/12/1999”);
myCar.milesofar = 13455;

Response.Write(myCar.Make + “</br>”);
Response.Write(myCar.Model + “</br>”);
Response.Write(myCar.orderdate + “</br>”);
Response.Write(myCar.milesofar + “</br>”);

In the snippet of code above I just create an instance of the class TheCar, mycar and I give values to the specific properties of mycar object. Then I just use the Write method of the Response object and output them in the screen. Nothing new up to this point.

But if we wanted to rewrite the code where we set the property values of our object we could do that by taking advantage of the new potential of the C# compiler and write the code above in one line.

TheCar mycar = new TheCar() { Make = “Mercedes”, Model = “2004”, milesofar = 8976 };

By doing this we have more concise code and the people over at Microsoft call object initialisers, syntactic sugar feature.

It is easy with object initialisers to add objects to collections.  If I wanted to add three cars to a generics-based List collection of type “TheCar”, I could write the below code:

List<TheCar> mycar = new List<TheCar>();
  mycar.Add( new TheCar { Make = “BMW”, Model = “2003”, milesofar = 3234} );
  mycar.Add( new TheCar { Make = “Mercedes”, Model = “2000”, milesofar = 8500 } );
  mycar.Add( new TheCar { Make = “Fiat”, Model= “2003”,milesofar = 5333 } );

The C# compiler supports what is widely referred as “collection initialisers”.We can write the above code like this. As you see we avoid the Add method.

List<TheCar> mycar = new List<TheCar>{
  new TheCar { Make = “BMW”, Model = “2003”, milesofar = 3234} ,
  new TheCar { Make = “Mercedes”, Model = “2000”, milesofar = 8500 } ,
  new TheCar { Make = “Fiat”, Model= “2003”,milesofar = 5333 }


It is a much easier(less typing for all those lazy developers out there!!!) and concise way to define objects and initialise them.

The compiler will preserve the same semantic meaning in the case of object and collections initialisers.If you use reflector to view the assembly file you will see that the MSIL generated is exactly like our old code. By saying old I mean it looks like the code we would have written to initialise an object before the c# 3.0 version became available.

Hope it helps

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl


1. Anonymous types in C# 3.0 « DOT NET RULES - July 7, 2008

[…] understand better about anonymous types I would suggest that you have a look at this post of mine that talks about object initialisers and this one that talks about implicitly local […]

2. VB 10.0 new features and Visual Studio 2010 « DOT NET RULES - June 27, 2009

[…] initialisers was a feature of C# 3.0 and basically it is a new syntax we can create a collection and populate it with an […]

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

<span>%d</span> bloggers like this: