Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# SetsRequiredMembers: Constructor Initialization

Learn how the C# SetsRequiredMembers attribute marks constructors that initialize all required members and when to use it.

~4 min readC#
#C##Required Members#Constructor#Compiler Attributes
Illustration of a C# constructor marked with SetsRequiredMembers attribute ensuring required members are initialized.

Starting with C# 11, the required modifier lets you declare that a field or property must be initialized when an object is created. Normally, callers must supply those members through an object initializer. If you write a constructor that already sets every required member, forcing callers to repeat that initialization is redundant. The SetsRequiredMembers attribute tells the compiler that a constructor takes responsibility for initializing all required members, so callers can use that constructor without an object initializer.

The Problem: Required Members and Object Initializers

Consider a class with a required property:

csharp
public class Person { public required string Name { get; init; } public required int Age { get; init; } }

Without any constructor, callers must initialize both members:

csharp
var person = new Person { Name = "Alice", Age = 30 };

If you add a constructor that sets these values, you might expect callers to use it directly:

csharp
public Person(string name, int age) { Name = name; Age = age; }

But the compiler still requires an object initializer when calling this constructor, because the constructor does not declare that it initializes all required members. The call new Person("Alice", 30) fails to compile with an error that tells you to set the required member in the object initializer or mark the constructor with SetsRequiredMembers.

Applying SetsRequiredMembers to a Constructor

The SetsRequiredMembers attribute is applied to a constructor to declare that it initializes all required members. The constructor must actually assign every required member; otherwise the compiler reports a diagnostic.

csharp
using System.Diagnostics.CodeAnalysis; public class Person { public required string Name { get; init; } public required int Age { get; init; } [SetsRequiredMembers] public Person(string name, int age) { Name = name; Age = age; } }

Now callers can construct the object without an object initializer:

csharp
var person = new Person("Alice", 30);

The attribute is a compile-time signal. It does not affect runtime behavior; it only suppresses the requirement for an object initializer.

How the Compiler Enforces Required Members

When a constructor is marked with SetsRequiredMembers, the compiler trusts that constructor to initialize every required member. If a required member is not assigned in the constructor body, the compiler reports a diagnostic. The attribute does not perform runtime validation; it is purely a compile-time contract.

The attribute can be applied to instance constructors, including parameterless constructors, as long as the constructor sets all required members. If a required member is not assigned, the diagnostic appears, and the attribute is effectively a false promise to the compiler.

When to Use SetsRequiredMembers

Use SetsRequiredMembers when you have a constructor that genuinely initializes every required member. This is common for DTOs, domain entities, and configuration objects where the constructor is the primary way to create instances. It removes the need for callers to repeat the initialization logic in an object initializer, which reduces duplication and makes the intent clearer.

The attribute is especially useful when the constructor performs validation or transformation of the input parameters before assigning the required members. For example:

csharp
public class Temperature { public required double Celsius { get; init; } [SetsRequiredMembers] public Temperature(double fahrenheit) { Celsius = (fahrenheit - 32) * 5 / 9; } }

Here the required member is set through a calculation, and the attribute tells the compiler that the constructor handles it.

Limitations and Common Pitfalls

The attribute is a promise, not a guarantee. If you mark a constructor with SetsRequiredMembers but forget to assign a required member, the compiler reports a diagnostic. The attribute itself does not perform runtime validation, so if the diagnostic does not stop the build, the unassigned member will have its default value at runtime. Always ensure the constructor assigns every required member.

Another pitfall is using the attribute on a constructor that calls a virtual method or helper that might not set the required members. The compiler cannot verify that the helper actually assigns the member, so you must be certain the assignment happens.

Inheritance and Derived Types

When a base class has required members, a derived constructor must also ensure they are initialized. Calling a base constructor marked with [SetsRequiredMembers] satisfies the base class required members. The derived constructor still needs to set any required members it declares, and if callers should be able to use that constructor without an object initializer, mark it with [SetsRequiredMembers] as well.

csharp
public class Employee : Person { public required string Department { get; init; } [SetsRequiredMembers] public Employee(string name, int age, string department) : base(name, age) { Department = department; } }

In this example, the base constructor sets Name and Age, the derived constructor sets Department, and the attribute on the derived constructor tells the compiler that all required members are initialized. If a constructor misses a required member, the compiler reports a diagnostic, even if it calls a base constructor marked with [SetsRequiredMembers].

Alternatives to SetsRequiredMembers

If you prefer not to use the attribute, you can keep the object initializer requirement. This may be useful when you want callers to explicitly name the members being set. Factory methods are another alternative: they can construct the object with an object initializer inside the factory, so the factory itself does not need the attribute. The choice depends on whether you want to enforce explicit member initialization at the call site or centralize it in a constructor.

Maintainability and Compatibility Considerations

SetsRequiredMembers is a compile-time feature. It does not do runtime validation, but it does create a maintenance contract: if you add a new required member, update every constructor marked with SetsRequiredMembers to initialize it. The compiler reports a diagnostic when a marked constructor misses a required member, but if the diagnostic is only a warning, it can be easy to miss unless you treat warnings as errors.

The attribute is available with C# 11 and .NET 7 and later. If you are targeting an older framework, you may need to provide a polyfill for the attribute or avoid required members in that codebase.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.