In this wiki let’s see how we can write a custom collection initializer. Before going straight to the topic let’s refresh our brains on Object Initializers and Collection Initializers.
Let’s say you have the following class named “Employee”.
“Employee” class has two properties “Id” and “Name”. So this is how we can initialize a object of type “Employee” and set the values for it’s properties in the classical way.
So notice here that there are three lines of code, and the more you have properties, the more lines of code you have to write just for setting up values for properties. Now let’s have a look at how we can do the same using the Object Initializer of the class Employee.
Even though there seems to be many lines of codes here, technically it’s just a one line. Note that even you can remove the parenthesis next to Employee and right the object initializer in the following way. It will still work.
Collection initializers works in the same way as Object Initializers and it lets you add one or more elements when you initialize a collection class that implements IEnumerable. The collection class must be implementing IEnumerable, otherwise you won’t be able to use collection initializer, please take a note of that. You will later understand why the collection has to be implementing IEnumerable.
Now let’s see how we used to add items to a collection in the classical way.
Here I have first created an List of type Employee and added a new Employee. And to add new Employee I have used it’s object initializer. Now let’s see how we can use the collection initializer to do the same.
So what happens here is by using a collection initializer you don't have to specify multiple calls to the Add method. Behind the scene when the source is compiling the compiler calls the Add method of the respective collection. So for that compiler needs the collection class to be implemented IEnumerable and to have an public method named Add. So here the List<T> is implementing IEnumerable.
So now let’s see how we can define a custom collection class (same is List<T>) which can be initialized using the collection initializer.
I am modifying the previous “Employee” class by adding a public constructor which takes two values in and set values to it’s properties.
Now I have the following Custom collection class named “EmployeeCollection” and it’s implementing IEnumerable of type Employee. Here I am adding a public method named "Add" which takes two parameters so as the constructor of the Employee.
Now let’s see my custom “EmployeeCollection” in action. By calling public Add method I can still add Employees.
And this is how we can use the collection initializer on my custom “EmployeeCollection”.
So that’s it. I am uploading the sample to my OneDrive.