Object and collection initializers

C# 3.0 NETFx 3.5

Published Updated Author Jeffrey T. Fritz Reading time

Object and collection initializers let you populate properties and collection elements inline at construction time. Instead of creating an object and then setting properties separately, you do it all in one readable expression.

C# 3.0 introduced object and collection initializers to eliminate the verbose pattern of creating an object, then assigning properties one by one. Now you can write new Person { Name = "Alice", Age = 30 } and build the entire object in a single, readable line.

Why it matters

Reads like a declaration, not a sequence of commands. Object initializers let you see all the properties and their values in one place. Your code becomes more declarative and easier to scan.

Eliminates temporary variables. Before initializers, you’d write:

var person = new Person();
person.Name = "Alice";
person.Age = 30;
return person;

Now you write:

return new Person { Name = "Alice", Age = 30 };

The second is clearer, shorter, and reads top-to-bottom.

Works great with LINQ and API calls. When you need to create a list of objects or build a request payload, collection and object initializers keep the code tidy and intent clear.

Supports nested structures naturally. You can nest objects and collections, building complex data structures inline without helper methods or intermediate variables. This is essential for working with JSON payloads, configuration objects, and test data.

Enables functional-style construction. Combined with var and LINQ, initializers let you build pipelines where objects are constructed, populated, and transformed in a single expression.

Cautions

They only work with parameterless constructors. If your class requires constructor arguments (especially logic-heavy ones), you can’t use initializers alone. You’ll need to combine them with a parameterized constructor or rethink your design.

Properties must be readable and writable (by default). Initializers set properties via reflection. If a property has only a getter, or is in an immutable record, you’ll need C# 9’s init accessor to use initializer syntax. In older C#, the property must have a setter.

Initialization order matters for nested objects. When building nested structures, the compiler initializes parent properties first, then children. If child objects depend on parent state during initialization, you may run into surprises. Keep dependencies simple or use constructors for complex setup.

They don’t call constructors for contained elements automatically. When you initialize a list like new List<int> { 1, 2, 3 }, the list’s constructor runs, but each element is added via Add(). This is usually fine, but be aware that element constructors don’t run during collection initialization—only during construction of the items themselves.

Validation happens after initialization. There’s a gap between when the object is constructed and when initializer properties are set. If you validate in the constructor, initializer-set properties won’t be validated. Use property setters with validation logic, or defer validation to a separate initialization step.

Not a substitute for proper constructors. Initializers are convenient, but objects with complex setup logic should use constructors. Don’t let initializer syntax tempt you to skip thoughtful, safe object construction.

Building objects with initializer syntax

Use object initializers to construct and populate an object in one readable expression.

Valid since C# 3.0

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var person = new Person { Name = "Alice", Age = 30 };

Initializing collections inline

Collection initializers let you populate lists, dictionaries, and other collections with values at construction time.

Valid since C# 3.0

var numbers = new List<int> { 1, 2, 3, 4, 5 };
var names = new[] { "Alice", "Bob", "Charlie" };

Nesting objects and collections

Combine object and collection initializers to build complex structures inline, keeping code readable.

Valid since C# 3.0

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
}

public class Person
{
    public string Name { get; set; }
    public Address Address { get; set; }
}

var person = new Person
{
    Name = "Alice",
    Address = new Address { Street = "123 Main St", City = "Seattle" }
};

Learn more

Object initializers (Microsoft Learn)