C# Required Property: Enforcing Initialization
Learn how the C# required modifier enforces property initialization at compile time, including object initializers, constructors, SetsRequiredMembers, and serialization considerations.

When a class has properties that must be set for the object to be valid, you previously had to rely on constructors or manual validation. The required modifier, introduced in C# 11, moves that check to compile time for ordinary object-creation expressions: the compiler requires all required members to be initialized, either in an object initializer or by calling a constructor marked with [SetsRequiredMembers]. This article explains how the C# required property feature works, where it applies, and what it means for constructors, serialization, and maintainability.
What the required Modifier Enforces
The required modifier is a compile-time contract. When you mark a field or property with required, the compiler ensures that every normal object construction path for the type initializes that member. The enforcement is static: the compiler does not emit runtime checks into the constructor or add reflection-based validation by itself. If an object-creation expression omits a required member and the invoked constructor is not marked [SetsRequiredMembers], the code does not compile.
This behavior closes a gap where objects could be created in an incomplete state. Previously, you might have written a constructor that accepted all mandatory values, but that approach becomes verbose when a class has many required fields. The required modifier lets you keep a parameterless constructor for object-initializer-based creation while still guaranteeing at compile time that certain properties are populated in the creation expression.
Declaring a Required Property
To declare a required property, place the required modifier before the property type. A required property must have a setter or an init accessor, cannot be static, and must be at least as accessible as the containing type. Here is a minimal example:
public class Product
{
public required string Name { get; set; }
public required decimal Price { get; set; }
public string? Description { get; set; }
}With this declaration, the following code compiles because both required properties are set in the object initializer:
var product = new Product
{
Name = "Laptop",
Price = 999.99m
};But this code fails to compile because Price is missing:
var product = new Product
{
Name = "Laptop"
};The compiler error is clear: Required member 'Product.Price' must be set in the object initializer or attribute constructor. This error appears at compile time, preventing the creation of incomplete objects through normal source code.
How Required Properties Interact with Constructors
Required-member enforcement is checked at the object-creation expression. For a constructor that is not marked with [SetsRequiredMembers], callers must set the required members in an object initializer. This is true even if the constructor body assigns the members; the attribute is what tells the compiler that a constructor satisfies the required-member contract. A parameterless constructor is therefore useful when you expect callers to use an object initializer.
public class Order
{
public required string OrderId { get; set; }
public required DateTime CreatedAt { get; set; }
public Order() { }
[SetsRequiredMembers]
public Order(string orderId)
{
OrderId = orderId;
CreatedAt = DateTime.UtcNow;
}
}With this declaration, the parameterless constructor can be called with an object initializer:
var order = new Order
{
OrderId = "1001",
CreatedAt = DateTime.UtcNow
};The second constructor is marked [SetsRequiredMembers], so it can be called without an object initializer:
var order = new Order("1001");If a constructor is not marked [SetsRequiredMembers], and a call site does not set all required members in an object initializer, the compiler reports an error such as Required member 'Order.CreatedAt' must be set in the object initializer or attribute constructor.
This behavior gives you flexibility. You can have a parameterless constructor for object-initializer-based construction while still enforcing that consumers set required properties. It also means that code using reflection to create an instance without setting required properties can produce an invalid object at runtime; the compiler cannot protect against reflection-based creation.
Using SetsRequiredMembers with Constructors
[SetsRequiredMembers] informs the compiler that a constructor initializes all required members, so callers do not need to set them in an object initializer. The compiler trusts the attribute and does not verify that the constructor body actually assigns every required member. This is useful when initialization is delegated to a helper or base constructor, but it places responsibility on you to ensure the constructor really sets every required member.
public class Employee
{
public required string Name { get; set; }
public required int Id { get; set; }
[SetsRequiredMembers]
public Employee(string name, int id)
{
Name = name;
Id = id;
}
}With the attribute, new Employee("Ada", 1) compiles. Without it, the compiler would require Name and Id to be set in the object initializer, even though the constructor body assigns them.
Use this attribute only when the constructor truly initializes all required members. If you misuse it, you reintroduce the possibility of creating an object with missing required values, defeating the purpose of the modifier.
Required Properties and Serialization
Serializers create objects outside normal object-creation syntax, so the compile-time requirement does not automatically cover them. However, from .NET 7 onward, System.Text.Json recognizes the required contract during deserialization: if a required member is missing from the JSON payload, deserialization throws a JsonException. When a parameterless constructor is available, the serializer typically creates the object and then populates its properties by name, but it checks required members before returning the object.
public class ApiRequest
{
public required string Endpoint { get; set; }
public required string Payload { get; set; }
}With System.Text.Json on .NET 7 or later, a missing Endpoint or Payload causes a JsonException rather than a silently incomplete object. If you use a serializer or custom converter that does not acknowledge required, or you target an older runtime, you still need a post-deserialization validation step. The required modifier is a developer-facing contract that helps prevent accidental omissions during coding, but data coming from external sources should be validated independently.
Compatibility and Maintainability Considerations
The required modifier is available in C# 11 and later, and you need a C# 11–capable compiler, such as the one in the .NET 7 SDK. The core enforcement is compile-time; the compiler emits metadata that aware tools and serializers can use at runtime, but older runtimes ignore that metadata. If you target an older .NET runtime, you can still use the feature with a recent compiler, but serializers on that runtime will not have the same awareness. Adding required to an existing class is a breaking source change because existing object initializers that omit the newly required property will fail to compile. In a large codebase, this often surfaces many compile errors, which is the desired outcome when callers need to address incomplete initialization.
For library authors, using required can improve the API surface by making it clear which properties are essential. However, it also limits flexibility for consumers who might want to create an object and set properties later. If a property is genuinely optional, do not mark it as required. Reserve the modifier for members that are always needed for the object to function correctly.
Edge Cases: Nullable and Default Values
A required property can be nullable or have a default value. The compiler checks only that the property is assigned in the object-creation expression; it does not use nullability or the default value to satisfy the requirement. For example:
public class Config
{
public required string? LogPath { get; set; }
public required int Timeout { get; set; } = 30;
}Even though LogPath is nullable, it must still be set. The default value for Timeout does not exempt it from the requirement. If you want a property to have a default and not require explicit assignment, do not mark it as required. The modifier is about the presence of an assignment, not the final value.
Another edge case is inheritance. When a base class has required properties, derived types must also result in those properties being initialized. A derived constructor can set inherited required properties, or call a base constructor marked [SetsRequiredMembers]; otherwise, call sites of derived types must set them in the object initializer. This can make constructor chains more complex, so plan how required members flow through inheritance hierarchies.
Finally, consider the interaction with init accessors. You can combine required with init to create immutable objects that must be fully initialized at creation time:
public class Point
{
public required int X { get; init; }
public required int Y { get; init; }
}This pattern is common for DTOs and value objects. The object cannot be modified after creation, and the compiler ensures that both coordinates are provided. This combination is particularly useful for record types, where init accessors are already the default.
The required modifier is a powerful tool for expressing object invariants at the type level. It shifts the responsibility of complete initialization from runtime checks to normal compilation, reducing accidental errors. When used thoughtfully, it improves code clarity and maintainability, but it requires careful consideration of serialization, inheritance, and existing construction patterns.
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.