Object and Collection Initializers Feature in C# 3.0


Introduction

C# 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.
 


Similar Articles