C# 3.0 Tutorials: Object and Collection Initializers

Howdy everybody! This is the second part of my C# 3.0 tutorial series in my blog. Today, we are gonna talk about object and collection initializers, one of the features of C# 3.0.

This feature allows you to initialize the entity object or collection in very easy way that we’ve never done before. When I was working for .NET 1.1 or 2.0 projects, I used to create at least three constructors in each and every entity classes just for making rich-constructors that helps the developers to initialize easily.

For example ~

public class Cat {
#region private variable declaration
private int _catID;
private string _name;
#endregion

#region constructors
public Cat() {
}
public Cat(string name) {
_name = name;
}
public Cat(int id, string name) {
_catID = id;
_name = name;
}
#endregion

#region properties
public int CatID{
get{
return _catID;
}
set{
_catID = value;
}
}

public string Name{
get{
return _name;
}
set{
_name = value;
}
}
#endregion

}

The reason why I created those constructors is that it make the developer’s life easier to initialize the object as below.

Cat cat = new Cat(1, "Pepsi Ko");
Cat anotherCat = new Cat("Ordico");

but just imagine that what if we have 100 entity classes with 20 or more properties. Creating three constructors for those classes would be time-consuming process, isn’t it? if you are a project manager of the team, you can simply ask your developers to add those constructors in each entity class. but sometime, you don’t have that level of control all the time. then, you will end-up writing the code below.

Cat cat = new Cat();
cat.CatID = 1;
cat.Name = "Pepsi Ko";

Cat anotherCat = new Cat();
anotherCat.Name = "Ordico";

With C# 3.0, you don’t need to write those constructors and you don’t need to ask anyone to create the constructors for you. You can simply initialize the object as below ~

Cat cat = new Cat { CatID = 1, Name = "Pepsi Ko" };
Cat anotherCat = new Cat { Name = "Ordico" };

It’s pretty cool, isn’t it? It saves some of your typing time and copy-n-paste works. :) If you are using VS 2008, you will get cool intellisense that can tell you what properties you have initialized.

VS Intellisense for Object Initializer

VS Intellisense for Object Initializer

If your entity class has the object of another class as a field then you can also initialize the contained class as below.

Let’s say you have the class like below ~

public class Cat {
public int CatID { get; set; }
public string Name { get; set; }
public List<Cat> Children { get; set; }
}

then, you can initialize like below ~

Cat mamamCat = new Cat { CatID = 1, Name = "Pepsi Ko",
Children = new List<Cat>{
new Cat{ CatID =11, Name = "Pussy Lay" },
new Cat{ CatID =12, Name = "Kitty" },
new Cat{ CatID =13, Name = "Soemasoe" }
}
};

All right. This is all about object and collection initializer. The advantage of using this feature is that it save some of your time for creating a lot of constructors or initializing the individual property. That’s all. If you have any comment or suggestion, please let me know. Thank you!

Posted in C#

2 thoughts on “C# 3.0 Tutorials: Object and Collection Initializers

Leave a Reply

Your email address will not be published. Required fields are marked *