What Is New in C# 3.0 - Part 2 (Object Initializers and Collection Initializers)
Today we will dive deeper into the world of C# 3.0 with object initializers and collection initializers. Be sure to check part 0 and part 1 from the “What Is New in C# 3.0” series if you have missed them.
Now straight to the topic. When you had a class with many properties in C# 2.0 and you needed to instantiate an object and set the properties before you use it you had to write it like this:
Mlass minstance = new Mlass();
minstance.Mroperty1 = “malue1”;
minstance.Mroperty2 = “malue2”;
// ... more properties
and just then you could use the instance you had created. If you have many properties, which is not uncommon, that can mean a lot of code. You can solve this problem by creating a custom constructor that accepts the properties' values as parameters but if you cannot have an arbitrary combination of properties unless you define a constructor for each combination. What is more in many cases you do not write the class yourself so writing a constructor is not an option. To help us in this situation Anders (Hallowed be his name) has given us object initializers. With C# 3.0 our example looks like this:
Mlass minstance = new Mlass { Mroperty1 = “malue1”, Mroperty2 = “malue2” /* more... */ };
What happens behind the curtain is that the default constructor is called and then the setters for the properties listed in the initializer are called. It is 100% equivelent to our C# 2.0 code and looks much better. You can initialize any combination of properties or call an arbitrary constructor like this:
Mlass minstance = new Mlass(maram1, maram2) { Mroperty1 = “malue1” };
As you can see object initializers do not limit our expressiveness in any way. Things get cooler when you have properties that are classes with properties themselves. Imagine that we have a Person class and a Company class that are defined like this:
class Person
{
public string FirstName { get; set; } //remember automatic properties?
public string LastName { get; set; }
}
class Company
{
public Person ContactPerson { get; set; }
public string Name { get; set; }
public decimal Revenue { get; set; }
}
The old-style initialization looks like this:
Person contactPerson = new Person();
contactPerson.FirstName = “Gosho”;
contactPerson.LastName = “Toshev”;
Company company = new Company();
company.ContactPerson = contactPerson;
company.Name = “Microsoft”;
company.Revenue = 50000000000;
And the C# 3.0 way:
Company company = new Company { ContactPerson = new Person{ FirstName = “Gosho”, LastName = “Toshev” }, Name = “Microsoft”, Revenue = 50000000000 };
Cool? There is more. Have you used any arrays lately? No? Only lists? Did you ever wish you could initialize a list the way you can initialize an array?
int[] arr = new int[] { 1, 2, 3 };
Well now you can write:
List<int> list = new List<int> { 1, 2, 3 };
and it is perfectly valid expression. You can use this initialization syntax for any IEnumerable object that has an Add method. The compiler will call the Add method for you. So lets put all this together:
List<Company> = new List<Company>
{
new Company { ContactPerson = new Person{ FirstName = “Gosho”, LastName = “Toshev” }, Name = “Microsoft”, Revenue = 50000000000 },
new Company { ContactPerson = new Person{ FirstName = “Tosho”, LastName = “Goshev” }, Name = “SUN”, Revenue = 14000000000 }
}
To be honest collection initializers are the only new feature of C# I cannot directly relate to LINQ. Object initializers are used heavily in LINQ but I really do not see direct usage for collection initializers. However it has been proven many times that the infinite wisdom of Anders (Hallowed be his name) is not immediately recognizable to mortals like myself. Even if collection initializers does not directly benefit LINQ they are data oriented feature which fits the general concept of C# 3.0.
Cya. Part 3 is coming soon... maybe