Desugaring object initializers

Do you sometimes shiver when you see yourself writing line after line of assignments when initializing a new object? Maybe something like this:

   
1:  Foo foo = new Foo();
2:  foo.A = 1;
3:  foo.B = 2;
4:  foo.C = 3;
5:  foo.D = 4;
6:  foo.E = 5;

As you can see, this syntax is quite verbose. Fortunately, C# added some syntactic sugar to make your life easier.

Meet the object initializer

Starting with C# 3 some syntactic sugar was added to help you with initializing objects. Using this new syntax the previous code can be changed to:

   
1:  Foo foo = new Foo
2:  {
3:      A = 1,
4:      B = 2,
5:      C = 3,
6:      D = 4,
7:      E = 5
8:  };

This syntax is called an object initializer. If you desugare this code you will see the following:

   
1:  Foo __foo = new Foo();
2:  __foo.A = 1;
3:  __foo.B = 2;
4:  __foo.C = 3;
5:  __foo.D = 4;
6:  __foo.E = 5;
7:
8:  Foo foo = __foo;

A local temporary variable is created by the compiler. In the generated IL code, this variable uses the special naming construct with angle brackets so you don’t get conflicts with your own code. Then the properties are assigned and the temporary variable is assigned to the actual variable.

On a side note, do you think the following the code compiles?

1:  Foo foo = new Foo
2:  {
3:      A = 1,
4:      B = 2,
5:  };

As you can see, you have a comma following the assignment of B. And yes, this code will compile! Why? The compiler allows the extra comma in object initializers to make code generation easier. You don’t have to check if you are generating code for the last property you want to set, you can jus add the comma.

Anonymous types

While object initializers are a nice form of syntactic sugar in these types of scenarios, they are actually required when working with anonymous types. An anonymous type is a type that is defined by the way it’s initialized:

1:  var myAnonymousType = new { X = 42 };

By using the object initialization syntax, you define a new property called X for your anonymous type. After this line, your object will have a property X. You can’t just add a new property to it by doing something like myAnonymousType.Y = “This won’t work”.

Why? Well, anonymous types are also a form of syntactic sugar. In a future blog post we will look at anonymous types in more detail but in essence, the compiler generates a class for you with an angle bracket name and the properties that you use in the object initializer.

So, that’s the idea behind object initializers. It’s just a nice way of quickly initializing a new object or creating a new anonymous type.

Feedback? Questions? Please leave a comment!

Wouter de Kort works as a lead architect and consultant. He helps organizations stay on the cutting edge of software development. Wouter focuses on DevOps. He loves solving complex problems and helping other developers to grow. Wouter authored the book DevOps on the Microsoft stack and a couple of other books. Wouter is a Microsoft MVP and an ALM Ranger. You can find him on Twitter (@wouterdekort), on his blog at wouterdekort.com and at the various conferences where Wouter speaks.

Share

Leave a Reply

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

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.