Thursday, December 06, 2007

Object & Collection Initializers: C# 3.0

Object & Collection Initializers are a new feature in C# 3.0. They effectively save you the need to write contructors on object initialization.

Firstly, we will talk about object initalizers. Take a look at the following code which is what you would normally write in C#:
//C# 2.0
public class Person
{
private string name = string.Empty;
private int age = -1;
private bool married = false;

public Person(string name)
{
this.name = name;
}

public Person(string name, int age)
{
this.name = name;
this.age = age;
}

public Person(string name, int age, bool married)
{
this.name = name;
this.age = age;
this.married = married;
}

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public bool Married
{
get
{
return married;
}
set
{
married = value;
}
}

public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
Nothing too complicated there or out of the ordinary.

It's a pain to have to code constructors for objects and especially when you add more properties to an object, in this case generally in the past you'd create a new constructor or modify an existing one. You needn't do this in C# 3.0 anymore as in order to set properties when we instantiate the object, we can simply set the public properties on the same line as we instantiate the object - Much like calling a constructor but without having to write any code! This is where the power of object intializers come into play with C# 3.0. Consider the following code:
//C# 3.0
public class Person
{
private string name = string.Empty;
private int age = -1;
private bool married = false;

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public bool Married
{
get
{
return married;
}
set
{
married = value;
}
}

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

In reference to the above code, in C# 2.0 there is no way of setting the objects properties at initialization time without having to call the properties explicitly after we have a reference to the object. Well in C# 3.0 now we can. This is how we do it in C# 3.0:
Person person = new Person
{
Name = "Simon",
Age = 30,
Married = true
};
You could also use the above with Implicitly Typed Local Variables which is another new feature of C# 3.0. I've written an article on this here.

So if I wanted to use the above with an Implicitly Typed Local Variables I could code the above as:
var person = new Person
{
Name = "Simon",
Age = 30,
Married = true
};
Notice the syntax is a little like Anonymous Types.

Intellisense is quite clever also, the compiler and intellisense will not allow you to define a property more than once, if you do, in this case defining name twice you will get a compiler error: "Duplicate initialization of member 'Name'". Once you have set a property, intellisense will omit that property from the list of available properties. Much like overriding methods that we have enjoyed since year dot.

Now that we have covered object initializers, we will talk about collection initializers.
List<Person> peopleCollection = new List<Person>
{
new Person{Name = "Simon Hart", Married=True, Age=30}
};
Nice and clean! How much easier is that. I like this new feature of C# as it makes my code simpler which then makes it easier to change and understand.

No comments: