Object initializers are awesome because they allow you to create an inline class. The trade-off is that your class cannot be immutable. Consider:
public class Album {
If a class is defined this way, it means that there really is no easy way to change the contents of a class after it is created. Invariability has advantages. When something is immutable, it is much easier to determine that it is correct. After all, if it cannot be modified after construction, then there is no way that it will ever be โwrongโ (after you determine the correctness of its structure). When you create anonymous classes, for example:
new { Name = "Some Name", Artist = "Some Artist", Year = 1994 };
the compiler will automatically create an immutable class (i.e. anonymous classes cannot be changed after construction), because immutability is so useful. Most C ++ / Java style guides often encourage the creation of const (C ++) or final (Java) members for exactly this reason. Large applications are much easier to check when the number of moving parts is less.
With everything said, there are situations when you want to quickly change the structure of your class. Let's say I have a tool that I want to customize:
public void Configure(ConfigurationSetup setup);
and I have a class that has several elements, such as:
class ConfigurationSetup { public String Name { get; set; } public String Location { get; set; } public Int32 Size { get; set; } public DateTime Time { get; set; }
Using object initializer syntax is useful when I want to configure some combination of properties, but not all at once. For example, if I just want to configure Name and Location , I can simply do:
ConfigurationSetup setup = new ConfigurationSetup { Name = "Some Name", Location = "San Jose" };
and this allows me to create some combination without having to define a new constructor for every possible permutation.
In general, I would say that turning your classes into a fixed state in the long run will save you a lot of development time, but with the object initializer syntax it will greatly simplify the configuration of certain configuration permutations.