One of the new language features in C# 3.0, thanks to the LINQ stuff, is the new syntax for object and collection initializers. Basically, this feature allows you to initialize an object by both creating the object instance (i.e. a new expression) as well as assign values to one or more properties in a single sentence, or to initialize a collection with a set of values to add to it in a single expression similar to how you already do with arrays in C# < 3.0.
Here's an example of using the new syntax:
Person tomas = new Person() { Name="Tomas", Age=28, Hobby="Music" };
Person clone = new Person() { tomas.Hobby, tomas.Name, tomas.Age };
List people = new List() { tomas, clone };
The first line initializes a Person object with the default constructor (though you could call a constructor with parameters here), and assigns explicit values to the Name, Age and Hobby properties.
The second line initializes another Person object with the default constructor, and again assigns values to all the properties. However, notice that here we don't especify the name of the properties to initialize explicitly. Instead, the compiler will "guess" which property to initialize by matching the name of the property of the object used in the expression (i.e. tomas.Hobby is matched to the Hobby property).
The third line initializes a collection of type List
This entire feature can be quite handy and I believe it can significantly improve readability of the code when used correctly. Personally, I prefer to use as much as possible the explicit form of it in which you clearly especify which property you're assigning a value to (i.e. Hobby=tomas.Hobby), particularly when using the very similar syntax to initialize anonymous types:
var minime = new { Name=tomas.Name, Age=tomas.Age/2 };
One thing did strike me as curious about the new object initialization syntax in C# 3.0: C# already had a special syntax that did something very similar: The syntax for attributes:
[Attribute(1, Property=value)]
While this was a very restricted syntax, it served much of the same purpose, and it was somewhat curious to me why a different syntax was used instead of reusing the existing one. That said, I think I can guess at some of the reasons this was not done:
- The new object initialization syntax is more inline with the array initialization syntax
- The new sytnax clearly separates what is being initialized as part of the object construction process (i.e. everything in the ()) and what is being initialized with property setters (i.e. everything in the {}).
Number 2 in particular might be important because it is more explicit when you introduce the whole "automatic matching of member being used for intitialization to the name of the member being initialized" feature. If the existing attribute initialization syntax had been reused, this would've been far more confusing as there would be no easy way to spot whether a given initializing expression was actually using one of the available constructors or it was implicitly initializing a given property, which would've significantly reduce the readability of the language in this cases.
That said, I don't care all that much for the automatic property name guessing; I much prefer to be explicit as to what property I'm initializing.