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)
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:
initintroduced for object initializers.