Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The required modifier indicates that the field or property it applies to must be initialized by an object initializer. Any expression that initializes a new instance of the type must initialize all required members. The required modifier is available starting with C# 11.
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
Tip
To find when a feature was first introduced in C#, consult the article on the C# language version history.
By using the required modifier, developers can create types where properties or fields must be properly initialized, yet still allow initialization through object initializers. Several rules ensure this behavior:
- Apply the
requiredmodifier to fields and properties declared instructandclasstypes, includingrecordandrecord structtypes. You can't apply therequiredmodifier to members of aninterface. - You can't mark explicit interface implementations as
required. You can't set them in object initializers. - You must initialize required members, but you can initialize them 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. Code that creates an instance can't set members that aren't accessible. - 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. - You can't use a type with any
requiredmembers 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. - You can't use the
requiredmodifier on the declaration for positional parameters on a record. You can add an explicit declaration for a positional property that does include therequiredmodifier.
Some types, such as positional records, use a primary constructor to initialize positional properties. If any of those properties include the required modifier, the primary constructor adds the SetsRequiredMembers attribute. This attribute indicates that the primary constructor initializes all required members. You can write your own constructor with the System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute attribute. However, the compiler doesn't verify that these constructors do initialize all required members. Rather, the attribute asserts to the compiler that the constructor does initialize all required members. The SetsRequiredMembers attribute adds these rules to constructors:
- 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.
Warning
The SetsRequiredMembers attribute disables the compiler's checks that all required members are initialized when an object is created. Use it with caution.
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; }
}
For more information on required members, see the C#11 - Required members feature specification.