Glossary / Required members

Required members

The required modifier marks a field or property that callers must initialize when creating an instance. It makes construction requirements visible to the compiler, but it is not runtime validation and does not guarantee that the supplied value is meaningful or non-null.

Required initialization

public class UserOptions
{
    public required string Name { get; init; }
}
var options = new UserOptions { Name = "Ada" };

The compiler reports an error when a creation expression omits a required member, unless a constructor is marked with SetsRequiredMembersAttribute or another language-defined rule says it establishes them. That attribute is a promise to the compiler; it does not inspect constructor behavior. Required members must be sufficiently accessible for callers to initialize them, and derived types cannot remove the requirement.

A required member can still be initialized to null; for a non-nullable reference property the compiler warns. Required is useful for DTOs and configuration objects with many named options. It may be awkward for serializers or APIs where invariants depend on multiple values; constructors or factories can provide stronger control.

Interview questions

  1. What does required enforce? Compile-time initialization at object creation sites.
  2. Does it validate at runtime? No.
  3. What does SetsRequiredMembers mean? It asserts that the constructor establishes required members; the compiler trusts it.
  4. Does required imply non-null? No; nullability and runtime validation are separate.
  5. When prefer a constructor? When invariants need controlled, atomic construction or validation.

References