C# Feature β Required members
Overview
Section titled βOverviewβrequired enforces that specified members must be set during object initialization.
Introduced In
Section titled βIntroduced InβC# 11 (2022)
public class User { public string Email { get; set; } /* risk of missing init */ }public class User { public required string Email { get; init; } }var u = new User { Email = "a@b.com" }; // requiredAnnotated Example
Section titled βAnnotated Exampleβpublic record Customer(string Id){ public required string Name { get; init; }}Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Use with
initfor immutable models.
Related Features
Section titled βRelated FeaturesβVersion history
Section titled βVersion historyβ- C# 11:
requiredintroduced. - C# 12+: Works smoothly with primary constructors.