C# Feature – Init-only setters
Overview
Section titled “Overview”init
enables immutable patterns by allowing property initialization in object initializers while preventing later mutation.
Introduced In
Section titled “Introduced In”C# 9 (2020)
Before
Section titled “Before”public class Person { public string Name { get; private set; } }
public class Person { public string Name { get; init; } }var p = new Person { Name = "Ada" };
Annotated Example
Section titled “Annotated Example”public record User(string Id){ public string Email { get; init; }}
Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Combine with records for immutable data models.
Related Features
Section titled “Related Features”Version history
Section titled “Version history”- C# 9:
init
introduced for object initializers.