IntroductionC# 3.0 introduced another interesting feature Object and Collection initialization expressions.If you take Employee Class as an Example:We can use Object Initialization Expressions in C#3.0 Features to create an Employee as follows:Now observe the above code we have neither invoked the constructor nor set the any properties directly. The code above is equivalent to the following code:We can use Collection Initialization Expressions in C#3.0 Features to create a list of Employees as follows:The advantage of using this feature is that it saves your time for creating a lot of constructors or initializing the individual property.
Object and Collection Initializers Feature in C# 3.0
Lambda Expressions in C# 3.0
It is probably a good idea to post the code with the article.
Hi
You are right. Only public setters can be used in the Object Intialization. Thanks for the notice. The ID property should have the Setter value and comma is missing after FirstName property.
Kalyan.B
The ID is a private field and it has no public setter, thus you cannot use object initialization, or am I missing something?