.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Collection Initializer in .Net Framework

Posted By:Dhananjay Kumar       Posted Date: September 05, 2010    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

Collection Initializer in .Net Framework 3.0 onwards. Collection initializer gives a simple syntax to create instance of a collection.

  1. Collection initializer is new feature of C# 3.0.

  2. Collection initializer gives a simple syntax to create instance of a collection.

  3. Any object that is implementing System.Collections.Generic.ICollectioncan be initialized with collection initializer.
Let us say, we have a class


public class Student
    public string FirstName { get; set; }
    public string LastName { get; set; }

Now if we want to make collection of this class and add items of type student in that collection and retrieve through the collection, we need to write below code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication16
    class Program
        static void Main(string[] args)
            List lstStudent = newList();
            Student std = newStudent();
            std.FirstName = "Dhananjay";
            std.LastName = "Kumar ";
            std = newStudent();
            std.FirstName = "Mritunjay ";
            std.LastName = "Kumar";
            foreach (Student resstd in lstStudent)




In above code,

  1. An instance of List of Student is getting created.

  2. An instance of the Student is getting created.

  3. Using the Add method on the list, instance of being added to list of students.

  4. Using For each statement iterating through the list to get the values.
Now, if instance of Student class can be assigned to List of student at the time of creation of instance of list then we call it automatic collection initializer.


If we see the above syntax

  1. It is highly readable.

  2. It is single statement.

  3. Instance of Student class is getting added on the fly.
And we can retrieve the values as below,


In retrieving implicit type local variable is being used to fetch the different instance of the student in list of student

List lstStudent = newList()
     newStudent{FirstName ="Dhananjay" ,LastName="Kumar"},
     newStudent {FirstName ="Mritunjay", LastName ="Kumar"}
foreach (var r in lstStudent)



How it internally works?

A collection initializer invokes the ICollection.Add(T) method for each specified element in order. In the above example , it will call

ICollection.Add(instance of Student )

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend