Welcome to DetlefGrohs.com Sign in | Join | Help

C# Object Initializers

I didn’t know about this language feature, but I really like it!!!

This

Pen pen = new Pen(Color) {Width = Size};

is equivalent to

Pen pen = new Pen(Color);
pen.Width = Size;

That is just so cool!!! It is a C# 3.0 feature that apparently even supports the creation of complete sub-objects. Very cool and very JSON like.

Published Monday, February 16, 2009 6:32 PM by detlef
Filed Under:

Comments

No Comments
Anonymous comments are disabled