Open In App

Object and Collection Initializer in C#

Last Updated : 11 Sep, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

In C#, object initializers and collection initializers are syntactic shortcuts that let you create objects and collections more concisely without explicitly calling multiple property setters or Add() methods.  

Object Initializer in C#

An object initializer allows you to create an object and set its properties in a single statement, without needing a constructor that takes parameters for every property.

Syntax:

ClassName obj = new ClassName

{

Property1 = value1,
Property2 = value2,
Property3 = value3

};

Example: In the below example, Geeks class doesn't contain any constructor, we simply created the object and initialized the value at the same time using curly braces in the main method. This initializing of values is known as object initializer.

CSharp
using System;

class Geeks {

    public string author_name
    {
        get;
        set;
    }
    public int author_id
    {
        get;
        set;
    }
    public int total_article
    {
        get;
        set;
    }
}

class GFG {

    static public void Main()
    {
        // Initialize fields using an object initializer
        Geeks obj = new Geeks() {
            author_name = "Ankita Saini", author_id = 102,
            total_article = 178
        };

        Console.WriteLine("Author Name: {0}", obj.author_name);

        Console.WriteLine("Author Id: {0}", obj.author_id);

        Console.WriteLine("Total no of articles: {0}", obj.total_article);
    }
}

Output
Author Name: Ankita Saini
Author Id: 102
Total no of articles: 178

Note: When the compiler compiles the above program it assign the values to the object as shown below:

Geeks __geeks = new Geeks();
__geeks.author_name = "Ankita Saini";
__geeks.author_id = 102;
__geeks. total_article = 178;

Geeks obj = __geeks;

Collection Initializer in C#

A collection initializer allows you to create a collection and populate it in a single statement, without calling Add() multiple times manually.

Syntax:

var collection = new CollectionType

{

item1,
item2,
item3

};

Example :

CSharp
using System;
using System.Collections;

class GFG {

    static public void Main()
    {
        // Creating another SortedListusing  collection, Initializer to initialize sortedlist
        SortedList my_slist = new SortedList() {
            { "b.09", 234 }, { "b.11", 395 },
                { "b.01", 405 }, { "b.67", 100 },
            {
                "b.55", 500
            }
        };

        foreach(DictionaryEntry pair in my_slist)
        {
            Console.WriteLine("{0} and {1}", pair.Key,
                              pair.Value);
        }
    }
}

Output
b.01 and 405
b.09 and 234
b.11 and 395
b.55 and 500
b.67 and 100

Key Points

1. You are allowed to initialize collection and the object at the same time.

Example:

CSharp
var author1 = new Geeks() {
    author_name = "Soniya", author_id = 103,
    total_article = 120
};

var author2 = new Geeks() {
    author_name = "Siya", author_id = 106,
    total_article = 90
};

var author3 = new Geeks() {
    author_name = "Arpita", author_id = 111,
    total_article = 130
};

List<Geeks> author
    = new List<Geeks>() { author1, author2, author3 };


2. You can also use null as an element in collection initializer.

Example:

CSharp
SortedList my_slist = new SortedList() {
    { 1.2, "Cat" }, { 1.3, null }, { 1.5, 234 },
};

Explore