I recently posted up about implicitly typed local variables in C# 3.0, the next feature I want to look at is object and collection initializers, however, before I go any further I want to point out the reason why I am looking at all these low hanging features.

Basically the small features that get introduced into a language are actually enablers for the more complex ones – otherwise, why would you bother? implicityly typed local variables enable anonymous types in LINQ query syntax – object and collection initializers are the same.

Object initializers are a shortcut syntax for instansiating and configuring an object instance. Lets take the following piece of valid C# code today:

  1 Customer c = new Customer();
  2 c.FirstName = "Joe";
  3 c.LastName = "Bloggs";
  4 c.Address = new Address();
  5 c.Address.Line1 = "42 Wallaby Way";
  6 c.Address.City = "Sydney";
  7 c.Address.State = "NSW";
  8 c.Address.PostCode = "2000";

With object initializers you could express this code as:

  1 Customer c = new Customer()
  2 {
  3       FirstName = "Joe",
  4       LastName = "Bloggs",
  5       Address = new Address()
  6       {
  7               Line1 = "42 Wallaby Way",
  8               City = "Sydney",
  9               State = "NSW",
 10               PostCode = "2000"
 11       }
 12 }

The interesting thing here is that this code actually takes more space – so is it clearer? Its probably too hard to tell from this example, lets look at collection initializers as well:

  1 Customer c = new Customer()
  2 {
  3       FirstName = "Joe",
  4       LastName = "Bloggs",
  5       Addresses = {
  6               new Address ()
  7               {
  8                       Line1 = "42 Wallaby Way",
  9                       City = "Sydney",
 10                       State = "NSW",
 11                       PostCode = "2000"
 12               },
 13               new Address ()
 14               {
 15                       Line1 = "10 Downing Street",
 16                       City = "London",
 17                       State = "England",
 18                       PostCode = "123-ABC"
 19               }
 20 };

In the above example the definition (use your imagination) for the Customer class has an addresses property and we are using a collection initializer to add elements to it. Collection initializers allow us to declare the elements of a collection (array or list) in one logical statement. In the case of initialising an array inline (which we can already do), the syntax becomes more compact:

  1 string[] strings = { "One", "Two", "Three" };

If you apply the implicit typing of local variables then it can be shortened even further to:

  1 var strings = { "One", "Two", "Three" };

Lists aren’t really any different except you need to be explicit about the type of list you are instansiating (note it doesn’t need to be generic, it could be ArrayList):

  1 List<string> strings = new List<string> { "One", "Two", "Three" };

But even this could be shorter:

  1 var strings = new List<string> { "One", "Two", "Three" };

Finally, something that I thought was interesting is that the following is invalid:

  1 public class Foo
  2 {
  3       public Foo Self;
  4 }
  5 
  6 public class Program
  7 {
  8       public static void Main()
  9       {
 10               Foo f = new Foo() { Self = f };
 11       }
 12 }

The C# compiler will barf because “f” is unassigned. But I feel that by the time the object initializer is executed it is. Its just one of those corner cases where it could go either way, so in C# they have locked it off with a compiler error. Next up we will look at anonymous types.