What do braces after C new statement do?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, curly braces {} after a new statement are called initializer syntax. They allow you to set properties, fields, or add elements to collections in a single expression without calling explicit setters or Add() methods. There are three types: object initializers (new Person { Name = "Alice" }), collection initializers (new List<int> { 1, 2, 3 }), and dictionary initializers (new Dictionary<string, int> { ["key"] = 1 }). The compiler translates these into regular property assignments and method calls.
Object Initializer
Object initializers set public properties or fields immediately after construction:
The compiler translates the object initializer into a constructor call followed by property assignments. It creates a temporary variable, sets all properties, and then assigns it to the target variable.
With Constructor Parameters
You can combine constructor arguments and initializer syntax:
Nested Object Initializers
Collection Initializer
Collection initializers add elements to any type that implements IEnumerable and has an Add() method:
Custom Collection Initializer
Any class with Add() and IEnumerable works:
Dictionary Initializer
Classic Syntax (C# 3+)
Index Syntax (C# 6+)
The index syntax uses the indexer (this[]) instead of Add(). The difference matters because Add() throws on duplicate keys while the indexer overwrites.
Array Initializer
Arrays use similar syntax but without new Type[] in some cases:
Anonymous Types
Object initializer syntax is required for anonymous types:
with Expression (Records, C# 9+)
Records use with and braces for non-destructive mutation:
Common Pitfalls
- Setting read-only properties in an initializer: Object initializers can only set properties with public setters (or
initsetters in C# 9+). Read-only properties or private setters cause a compile error. Use constructor parameters for read-only values. - Assuming initializer order is guaranteed: The C# specification guarantees that initializers execute in the order written, but relying on order (where one property depends on another) makes code fragile. Keep initializations independent.
- Using collection initializer without
Addmethod: The collection initializer syntax requires anAddmethod andIEnumerableimplementation. A class with onlyAppend()orInsert()will not work with{ }syntax. - Confusing dictionary
Addand indexer initializers:{ "key", value }callsAdd()and throws on duplicates.["key"] = valuecalls the indexer and overwrites silently. Choose based on whether duplicates are an error or expected. - Mixing constructor and initializer for the same property: Setting a property in both the constructor and the initializer means the initializer wins (it runs after the constructor). This can hide bugs if the constructor value is intended to be final.
Summary
{ }afternewenables object initializers (set properties), collection initializers (add elements), and dictionary initializers- Object initializers compile to property assignments after the constructor call
- Collection initializers call
Add()for each element in the braces - Dictionary initializers support both
{ key, value }(Add) and[key] = value(indexer) syntax - Anonymous types and
withexpressions on records also use brace syntax

