C# Reference ยท Keywords and Statements
The required modifier
Reference for the C# required modifier: force callers to set properties and fields in object initializers, SetsRequiredMembers, inheritance rules and errors.
required (C# 11) marks a field or property that every caller must initialize when it creates the object. The compiler checks this at each new expression: if the object initializer does not assign the member, the code does not compile. You get the convenience of object initializers without losing the guarantee that essential data is present.
Before required, the only way to force a value was a constructor parameter. That still works, but a constructor with eight parameters is hard to read. A required property keeps the call site self-documenting.
public class User { public required string Name { get; init; } public int Age { get; set; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public required Type Name { get; set; } public required Type Name { get; init; } public required Type field; [SetsRequiredMembers] public Ctor(...) { ... }
| Part | Description |
|---|---|
required |
Placed with the other modifiers on a non-static field or property of a class, struct or record. |
| Initializer | Every new of the type must assign the member in an object initializer, unless a constructor carries the attribute below. |
SetsRequiredMembers |
An attribute from System.Diagnostics.CodeAnalysis placed on a constructor. It tells the compiler that the constructor assigns all required members, so callers do not have to. The compiler does not verify the claim. |
Errors: CS9035 when a required member is not set; CS9040 when a type with required members is used as a type argument for a new() constraint; CS9032 if the member, or its setter, is less visible than the containing type (for example private set).
Usage notes
- Accessibility. A required member, and its setter or
initaccessor, must be at least as visible as the containing type. Aprivate setor aninternalmember on a public type is an error. - Inheritance. Required members are inherited. A derived class may add its own, and may override a required property with
required override, but must keep it required. - Not a null check.
requiredforces the assignment but not a non-null value.new User { Name = null! }compiles. Combine it with non-nullable reference types so the compiler warns about explicit nulls. - Not for deserializers by itself.
System.Text.Jsonhonorsrequiredsince .NET 7: a missing property throws aJsonExceptionduring deserialization. - Generic
new()constraint. A type with required members cannot satisfywhere T : new(). - Compared with constructors. Constructor parameters give positional, verifiable initialization and work everywhere;
requiredgives named, order-independent initialization and fits DTOs and configuration objects. - Records. Positional records already require their parameters. Use
requiredon properties that you add in the body. - Compatibility. The feature is implemented through attributes, so older compilers see a plain property and do not enforce anything.
Related
- init
- new
- record
- Tutorial: Properties, Constructors