required modifier (C# Reference)
The required modifier indicates that the field or property it's applied to must be initialized by all constructors or using an object initializer. Any expression that initializes a new instance of the type must initialize all required members. Constructors indicate that they initialize all required members by adding the SetsRequiredMembers attribute in the constructor declaration. Code that uses a constructor without this attribute must use object initializers to initialize all required members. The required modifier is available beginning with C# 11.
The required modifier and the SetsRequiredMembers attribute enable developers to create types where properties or fields must be properly initialized, yet still allow initialization using either constructors or object initializers. Several rules ensure this behavior:
- The
requiredmodifier can be applied to fields and properties declared instruct, andclasstypes, includingrecordandrecord structtypes. Therequiredmodifier can't be applied to members of aninterface. - Explicit interface implementations can't be marked as
required. They can't be set in object initializers. - Required members must be initialized, but they may be initialized to
null. If the type is a non-nullable reference type, the compiler issues a warning if you initialize the member tonull. The compiler issues an error if the member isn't initialized at all. - Required members must be at least as visible as their containing type. For example, a
publicclass can't contain arequiredfield that'sprotected. Furthermore, required properties must have setters (setorinitaccessors) that are at least as visible as their containing types. Members that aren't accessible can't be set by code that creates an instance. - Derived classes can't hide a
requiredmember declared in the base class. Hiding a required member prevents callers from using object initializers for it. Furthermore, derived types that override a required property must include therequiredmodifier. The derived type can't remove therequiredstate. Derived types can add therequiredmodifier when overriding a property. - A type with any
requiredmembers may not be used as a type argument when the type parameter includes thenew()constraint. The compiler can't enforce that all required members are initialized in the generic code. - A constructor that chains to another constructor annotated with the
SetsRequiredMembersattribute, eitherthis(), orbase(), must also include theSetsRequiredMembersattribute. That ensures that callers can correctly use all appropriate constructors. - Copy constructors generated for
recordtypes have theSetsRequiredMembersattribute applied if any of the members arerequired. - The
requiredmodifier isn't allowed on the declaration for positional parameters on a record.
The following code shows a class hierarchy that uses the required modifier for the FirstName and LastName properties:
public class Person
{
public Person() { }
[SetsRequiredMembers]
public Person(string firstName, string lastName) =>
(FirstName, LastName) = (firstName, lastName);
public required string FirstName { get; init; }
public required string LastName { get; init; }
public int? Age { get; set; }
}
public class Student : Person
{
public Student() : base()
{
}
[SetsRequiredMembers]
public Student(string firstName, string lastName) :
base(firstName, lastName)
{
}
public double GPA { get; set; }
}
Feedback
Submit and view feedback for