C# Required Keyword: Syntax and Usage
Learn how the C# required keyword enforces mandatory property initialization, its compile-time behavior, and practical patterns for constructors.

The C# required keyword, introduced with C# 11 and supported by the .NET 7 SDK, marks a field or property as mandatory for object initialization. When you apply required to a member, the compiler requires that member to be set during object creation. Callers can satisfy the requirement with an object initializer, or a constructor can initialize the member itself. If a constructor does not initialize every required member, callers must fill the remaining members with an object initializer. The SetsRequiredMembers attribute tells the compiler that a constructor takes responsibility for all required members. This shifts missing-data errors from runtime to compile time, catching incomplete initialization before the application starts.
Declaring Required Members
To declare a required member, add the required modifier to an instance field or property declaration. For example:
public class Product
{
public required string Name { get; set; }
public required decimal Price { get; set; }
}When you create a Product, the compiler enforces that both Name and Price are set:
var product = new Product
{
Name = "Laptop",
Price = 999.99m
};If you omit one of the required members, the code will not compile. The compiler emits an error indicating the missing member. This behavior is particularly useful for data transfer objects, configuration models, and other types where incomplete data should be rejected as early as possible.
Constructor Interaction with Required Members
Types with required members add a subtlety to constructor design. A constructor that assigns a required member satisfies that member. For example:
public class Product
{
public required string Name { get; set; }
public required decimal Price { get; set; }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
}This constructor is valid because it assigns both required members. If it assigned only Name, callers would have to supply Price in an object initializer; omitting Price at the call site would fail to compile.
When a constructor does not initialize every required member, the compiler demands that callers use an object initializer to fill the gaps. That means a constructor like this:
public Product(string name)
{
Name = name;
}forces the caller to write:
var product = new Product("Laptop")
{
Price = 999.99m
};If you want to bypass that requirement entirely, you can use the SetsRequiredMembers attribute on the constructor:
public class Product
{
public required string Name { get; set; }
public required decimal Price { get; set; }
[SetsRequiredMembers]
public Product() { }
}This attribute tells the compiler that the constructor takes responsibility for initializing all required members. It is up to the developer to ensure that the constructor actually does so; the compiler does not verify the body. Misusing it by leaving a required member unset results in runtime NullReferenceException or an uninitialized value when the member is accessed later.
Validation at Compile Time vs. Runtime
The primary advantage of the required keyword is moving mandatory-field validation from runtime to compile time. Without it, you might write a runtime check in a constructor or method:
public Product(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Name is required.", nameof(name));
Name = name;
}This check runs every time an object is created, and missing data may only surface as an exception. With required, the compiler rejects the code if a member is not initialized. This is more predictable because the error appears at build time, and it can avoid the need for null checks when the known list of members is the only requirement.
However, required only guarantees that a value is assigned. It does not validate the value's content. For example, a string property marked required could still be set to an empty string. For semantic validation, you still need runtime checks in constructors or property setters.
Interplay with Inheritance and Interfaces
Required members behave differently across inheritance hierarchies. If a base class has a required member, derived classes must also initialize it. Consider:
public abstract class Vehicle
{
public required string Model { get; set; }
}
public class Car : Vehicle
{
public required int Doors { get; set; }
}When constructing a Car, the compiler requires both Model and Doors to be set:
var car = new Car
{
Model = "Sedan",
Doors = 4
};If the base class declares a property as required, the derived class cannot remove that requirement. A derived type can also add new required members, in which case construction of that derived type is checked for the extra members. This behavior ensures that code which expects a base type can rely on the base class's required member being initialized.
Interfaces also have a limitation: you cannot mark an interface property as required; the required modifier is allowed only on class and struct members. An implementing class can mark a property as required, and construction of that concrete type is checked. But when code uses the interface type, that required modifier is not part of the interface contract. For example:
public interface IProduct
{
string Name { get; set; }
}
public class Product : IProduct
{
public required string Name { get; set; }
}If you instantiate Product directly, the compiler enforces Name. But if an API accepts or returns IProduct, the interface does not expose a required constraint, so the guarantee is not visible through that abstraction. If you need initialization guarantees through an abstraction, prefer a factory method or other construction pattern that returns a fully initialized instance.
When Required Members Are a Good Fit
The required keyword is most beneficial for types that are primarily used with object initializers. Examples include configuration option classes, data transfer objects, request models, and view models. These types often have many properties, and forgetting one can lead to subtle bugs. Marking them required converts an occasional runtime error into a compile-time error.
However, for domain entities that have rich behavior and invalidation logic, constructors remain the better choice. A constructor can enforce invariants and ensure that the object is fully valid after creation. Instead of relying on required, you can have a constructor that takes all necessary parameters:
public class Product
{
public string Name { get; }
public decimal Price { get; }
public Product(string name, decimal price)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Name cannot be empty.", nameof(name));
if (price < 0)
throw new ArgumentOutOfRangeException(nameof(price), "Price cannot be negative.");
Name = name;
Price = price;
}
}This approach ensures that every Product instance is fully initialized and valid. With required, you could have a property setter with validation, but it would be more verbose and harder to maintain across many properties.
Use required when you value the convenience of object initializers and compile-time enforcement, but accept that some validation may still need runtime checks.
Refactoring Implications and Maintainability
Adding required to an existing property is a breaking change for callers that create instances of that type without setting the new required member. Every object initializer that omits it will fail to compile. This is intentional but can be disruptive in large codebases. Before marking a member required, consider who uses the type and whether all those call sites can be updated.
Removing required from a member is non-breaking; existing code that sets the member will still compile. This asymmetry means you can loosen requirements without breaking callers, but tightening requirements later is a deliberate breaking change that needs coordination.
Another maintainability concern is the SetsRequiredMembers attribute. If you use it on a constructor to bypass the compiler check, you lose the compile-time safety net. Ensure that the constructor truly initializes every required member. A common mistake is to add SetsRequiredMembers to a parameterless constructor and then forget to initialize a required member. The result is a compile-time pass but a runtime NullReferenceException when you access the missing member.
A better approach is to avoid SetsRequiredMembers altogether and rely on the compiler to enforce initialization. If you need a parameterless constructor for serialization or other framework requirements, you might be better off not marking members as required and instead validating in that constructor or a custom setter.
Runtime Cost and Performance Considerations
At runtime, the required keyword has no direct cost. It does not emit any additional IL instructions for validation; it only influences compile-time analysis. If using required lets you remove a null check that a constructor previously performed, the generated code can be smaller or faster. In practice this benefit is often small because null checks are cheap, and required does not validate the value's content.
The main performance consideration is not the keyword itself but the initialization pattern. Object initializer syntax assigns properties after the constructor runs. If your type has a constructor that performs complex logic, that logic executes before the required members are set. This is the same as with any object initializer. There is no special performance penalty associated with required.
SetsRequiredMembers does not make generated code faster. It simply tells the compiler that the constructor initializes all required members. Relying on it without actually doing so can introduce runtime null-reference bugs, so it is not worth using for micro-optimization.
Limitations Across C# Versions and Tooling
The required keyword is a C# 11 feature, so it requires a toolchain that supports C# 11 (for example, .NET SDK 7 or later). If your project does not use the default C# version associated with its target framework, you may need to set the LangVersion property to 11 to use it. In .NET 7 and later projects, C# 11 is the default, so no extra configuration is needed.
Another limitation is that required members that cannot be assigned from an object initializer, such as readonly fields or getter-only properties, must be initialized in a constructor. For example, this class has neither a constructor assignment nor an object initializer path for Name, so it will not compile:
public class Product
{
public required readonly string Name; // Error
}In practice, required works best with read-write or init members.
Records have their own initialization story. A positional record already requires the primary constructor parameters, so you often do not need required for those values. You can still use required on other record properties, but the primary constructor is usually the clearer way to express mandatory initialization.
Finally, the compiler cannot enforce required members through dynamic binding, because the required check is a compile-time check. Anonymous types also cannot carry the required modifier. Understanding these boundaries helps you decide when required is the right tool and when another pattern gives you the same guarantees with less friction.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.