C# Object Initializers Syntax Explained

This article provides a comprehensive overview of object initializer syntax in C#, explaining what it is, how it works, and why it is used. You will learn the core syntax rules, see practical code comparisons between traditional assignment and object initializers, and discover how this feature integrates with modern C# features like anonymous types and init-only setters.

What Are Object Initializers?

Object initializers in C# allow you to assign values to any accessible fields or properties of an object upon creation without explicitly invoking a multi-parameter constructor. Introduced in C# 3.0, this syntax simplifies object creation, improves code readability, and eliminates the need to write multiple overloaded constructors purely for field assignment.

Basic Syntax

To use an object initializer, append an opening and closing brace containing comma-separated property assignments to your new expression:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

// Using an object initializer
Person person = new Person
{
    FirstName = "Jane",
    LastName = "Doe",
    Age = 28
};

When using an object initializer with a parameterless constructor, the parentheses after the type name are optional:

Person person = new Person { FirstName = "Jane", LastName = "Doe", Age = 28 };

Traditional Instantiation vs. Object Initializers

Before object initializers, initializing properties required either verbose parameter-heavy constructors or multiple sequential assignment statements:

Traditional Approach:

Person person = new Person();
person.FirstName = "Jane";
person.LastName = "Doe";
person.Age = 28;

Object Initializer Approach:

Person person = new Person
{
    FirstName = "Jane",
    LastName = "Doe",
    Age = 28
};

Under the hood, the compiler creates a temporary variable, invokes the parameterless constructor, sets the specified properties, and then assigns the result to your variable.

Combining with Constructors

Object initializers can be combined with constructors that accept parameters. The constructor executes first, followed by the property assignments inside the braces:

public class Employee
{
    public int Id { get; }
    public string Department { get; set; }

    public Employee(int id)
    {
        Id = id;
    }
}

// Constructor arguments in parentheses, followed by the initializer
Employee emp = new Employee(101)
{
    Department = "Engineering"
};

Anonymous Types and Init-Only Setters

Object initializers are essential when working with anonymous types, as they define the properties and values simultaneously:

var product = new { Name = "Laptop", Price = 999.99 };

With C# 9 and later, object initializers also support init accessors, allowing properties to be immutable after creation:

public class Book
{
    public string Title { get; init; }
    public string Author { get; init; }
}

// Valid at instantiation:
Book book = new Book { Title = "1984", Author = "George Orwell" };

// Compile error: Cannot modify init-only property outside of construction
// book.Title = "Animal Farm";

Key Benefits

  • Readability: All initial property values are grouped cleanly within the creation statement.
  • Flexibility: You can assign any combination of accessible properties without defining multiple constructor overloads.
  • Immutability: Works seamlessly with init-only properties to create immutable objects cleanly.