This is the my fourth post in the series of posts I am making on C#3.0. See the previous posts here, here and here

Object and collection initializers are new features in C#3.0 which is syntactic sugar over how you allocate and initialize objects and collections.

Object Initializers

Lets consider a class

class Customer

{

public string name;

public string address;

int age;

public int Age { get { return age; } set { age = value; } }

}

if I wanted to create a object of the type customer and fill up the public variables and properties then in C#2.0 I need to do something like

Customer customer = new Customer();

customer.name = "Abhinaba Basu";

customer.address = "1835 73rd Ave NE, Medina, WA 98039";

customer.Age = 99;

With the new object initializer syntax it possible to do all of the above in one statement as in

var cust = new Customer{name = "Abhinaba Basu", 
address =
"1835 73rd Ave NE, Medina, WA 98039",
Age = 99 };

This not only reduces lines of code but increases flexibility a lot. So no more being forced to write 5 overloads of a contructor that just accepts these variables and assigns them to fields.This syntax works for both public fields and properties.

In case the class contains other classes as fields then the same statement can also initialize the contained class (see code below in bold).

class Phone

{

public int countryCode;

public int areaCode;

public int number;

}

class Customer

{

public string name;

public string address;

public Phone phone;

}

static void Main(string[] args)

{

var cust = new Customer{name = "Abhinaba Basu",
address =
"1835 73rd Ave NE, Medina, WA 98039",

phone = new Phone {countryCode = 1, areaCode = 425,
number = 9999999}};

}

This piece of code is equivalent to the following C#2.0 code

Customer customer = new Customer();

customer.name = "Abhinaba Basu";

customer.address = "1835 73rd Ave NE, Medina, WA 98039";

Phone __phone = new Phone();

__phone.countryCode = 1;

__phone.areaCode = 425;

__phone.number = 9999999;

customer.phone = __phone;

Collection Initializers

Collection initializer is a natural extension to object initializer. It consists of a list of initilializer for each of the elements in a collection seperated by comma and encosed in {}. For a simple List<int> it is as follows

List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

For the Customer and Phone number class discussed above the collection initializer will look like

List<Customer> custList = new List<Customer> {

new Customer {

name = "Samrat", address = "NJ, USA",

phone = new Phone {countryCode = 91, areaCode = 999, number = 8888888}

},

new Customer {

name = "Kaushik", address = "Behala, Kolkata",

phone = new Phone {countryCode = 91, areaCode = 33, number = 3333333}

},

new Customer {

name = "Rahul", address = "Kerala",

phone = new Phone {countryCode = 91, areaCode = 00, number = 4444}

}

};

Just trying to write the above piece of code in C#2.0 syntax shows how cool this new feature is.

Why I love this

This feature reduce typing a lot and will go a long way in helping reduce carpal tunnel syndrome :^)

Moreover, it is very intuitive, so much so that people will start using this even without figuring out that this was not present before and they were required to allocate the collection and then individually allocate and add each item to the collection. Just after using it for a couple of days I am badly missing it in C#2.0. I just can't figure out why this was not considered for 2.0. I can guess why but I'll still complain :)