• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Dotnet Stuff

  • Home
  • Articles
  • Tutorials
  • Forums
  • Career Advice
  • Jobs
  • .NET FAQs
  • News
  • Privacy Policy

Collection Initializers in C# 3.0

 C# Collection Initializers enable you to add items in the collection at the time of instantiation of the collection. Collection can be a List or Dictionary or any other available C# collection.

Till C# 2.0, for creating a collection of Employees we normally do

List<Employee> employees = new List<Employee>();
employee.Add(new Employee("EMP001","Sam","Bangalore,India",20000));
employee.Add(new Emploee("EMP00","Peter","Bombay,India",30000));
employee.Add(new Emploee("EMP003","Tom","Delhi,India",40000));

Assuming that there is an overloaded constructor for the Employee class.
Here above, each Employee object is added one by one after creating the collection instance.

In C# 3.0 by the use of collection Initializers, it is possible to declare and initialize the collection in line directly as below.

List<Employee> employees = new List<Employee>()
{
    new Employee("EMP001","Sam","Bangalore,India",20000),
    new Emploee("EMP002","Peter","Bombay,India",30000)
    new Emploee("EMP003","Tom","Delhi,India",40000)
};

Similarly, any other collection can be declared. See a dictionary example below

Dictionary<string,Employee> employeeDictionary 
                          = new Dictionary<string,Employee>()
{
     {"Emp1",new Employee("EMP001","Sam","Bangalore,India",20000)},
     {"Emp2",new Employee("EMP002","Tom","Delhi,India",40000)}
};

 

Related Posts:

  • C# 3.0 Object Initializers
    C# 3.0 Object Initializers

Related posts:

  1. C# 3.0 Object Initializers
  2. How to add text in a created table in a richtextbox?
  3. JSON Tutorial – Learn JSON Quickly
  4. How To Select an XML Node Element Values According to a Specific Attribute Value – C#,VB.NET

Related Posts:

  • C# 3.0 Object Initializers

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?
Log In
Increase Your Leadership Skills, Influence, and Power with Top Leadership courses starting at just $11.99.

Recent Posts

  • .NET Core Version History | Release History of .NET Core
  • What is string interpolation in C#
  • What Is The Use Of Volatile Keyword In C#
  • Get System IP Address Using C#
  • SQL Server Interview Question and Answers
  • C# Fixed Size Buffer
  • OWIN in ASP.NET Core| What is OWIN in .NET?
  • C# Version History | C# Evolution and Features
  • What is ASP.NET Core Module
  • C# 3d array

Recent Forum Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

Forums

  • .NET Framework
  • ASP.NET
  • C#
  • SQL

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

© Copywright 2017 Dotnetstuffs All Rights Reserved