Select Page

A required member makes callers participate in initialization. It does not prove that a constructor establishes a valid object. This distinction matters when you add constructors, inherit a model, or pass it to a generic factory.

This tutorial focuses on those construction boundaries. For the object initializer basics and assignment timing, start with our init-only properties tutorial. Here you will learn when an initializer remains mandatory, what a constructor attribute promises, and when a validating constructor is the clearer API.

Verified October 19, 2026 with .NET SDK 10.0.402, target framework net10.0 and C# 14.0, using separate console programs with nullable analysis and warnings treated as errors. Required members were introduced in C# 11; these examples use no C# 14-specific syntax. See the required reference and language version configuration guide.

A member default does not satisfy the caller’s obligation

It is tempting to add a sensible default and assume that callers can now omit the member. Compile this complete program on its own:

var order = new Order();
public class Order
{
    public required int Quantity { get; init; } = 1;
}

The compiler reports CS9035. Although the property initializer supplies 1, the constructor still advertises that callers must initialize Quantity. For this construction, adding an object initializer that assigns Quantity satisfies that obligation. The required-members specification describes the contract attached to constructors.

Assigning in a constructor is not the same as declaring a promise

This constructor assigns Destination, but the call still fails with CS9035:

var route = new Route("depot");
public class Route
{
    public required string Destination { get; init; }
    public Route(string destination) => Destination = destination;
}

The compiler does not inspect the body and automatically waive the required-member contract. A constructor that takes responsibility for all required members can declare that promise with SetsRequiredMembers, from System.Diagnostics.CodeAnalysis. In a hierarchy, that responsibility includes inherited members.

using System.Diagnostics.CodeAnalysis;

var parcel = new TrackedParcel("depot", "PK-17");
Console.WriteLine($"{parcel.Destination}: {parcel.TrackingCode}");
var redirected = new TrackedParcel("depot", "PK-18")
{
    Destination = "office"
};
Console.WriteLine(redirected.Destination);

public class Parcel
{
    public required string Destination { get; init; }
    [SetsRequiredMembers]
    public Parcel(string destination) => Destination = destination;
}
public class TrackedParcel : Parcel
{
    public required string TrackingCode { get; init; }
    [SetsRequiredMembers]
    public TrackedParcel(string destination, string trackingCode)
        : base(destination) => TrackingCode = trackingCode;
}

Expected and observed output:

depot: PK-17
office

The base constructor initializes Destination and the derived constructor initializes TrackingCode. Both declare the promise, so neither call needs a required-member initializer. The second call shows that an initializer can still replace Destination after constructor assignment. If the constructor validates an argument, validation of that argument alone cannot protect a publicly initializable property from a later initializer value.

Microsoft’s required reference documents the attribute and its chaining rule: a constructor that calls an annotated base or sibling constructor must carry the attribute too. In the following deliberately unannotated Child constructor, the compiler reports CS9039 on the constructor and CS9035 at new Child(). The call-site error occurs because the unannotated constructor still requires callers to initialize Value:

using System.Diagnostics.CodeAnalysis;

_ = new Child();
public class Parent
{
    public required int Value { get; init; }
    [SetsRequiredMembers]
    public Parent() => Value = 1;
}
public class Child : Parent
{
    public Child() : base() { }
}

When maintaining a hierarchy, review the full member list whenever a base or derived type gains a required member. An old annotation still asserts that the constructor handles the whole contract.

The attribute is trusted, not checked for complete assignment

This deliberately incorrect promise compiles successfully:

using System.Diagnostics.CodeAnalysis;

Console.WriteLine(new Stock().Quantity);
public class Stock
{
    public required int Quantity { get; init; }
    [SetsRequiredMembers]
    public Stock() { }
}

Expected and observed output:

0

Quantity remains zero. The attribute removes the caller-side initialization check without proving that every required member was assigned. This example uses an integer to isolate that behavior: nullable analysis can separately warn about an uninitialized non-nullable reference member in an annotated constructor. The specification’s constructor and nullable-analysis sections distinguish these checks.

Only apply the attribute when the constructor really initializes every required member, and test that responsibility. Do not add it merely to silence an error at a call site.

Required assignment and valid data are separate checks

An assignment can satisfy the required-member check while nullable analysis rejects its value. With the verification project’s warnings-as-errors setting, this program fails with CS8625:

_ = new Address { City = null };
public class Address
{
    public required string City { get; init; }
}

Normally that nullable diagnostic is a warning. It is distinct from the omission error CS9035. A nullable required property can be explicitly initialized to null. Treat the assignment requirement as one part of an API contract, and validate domain rules separately. These rules are documented in the required reference.

If a positive quantity must always be established by the public construction API, a constructor with a get-only property expresses that rule directly:

var order = new Order(3);
Console.WriteLine(order.Quantity);
try
{
    _ = new Order(0);
}
catch (ArgumentOutOfRangeException error)
{
    Console.WriteLine(error.ParamName);
}
public class Order
{
    public int Quantity { get; }
    public Order(int quantity)
    {
        if (quantity <= 0)
            throw new ArgumentOutOfRangeException(nameof(quantity));
        Quantity = quantity;
    }
}

Expected and observed output:

3
quantity

The caller supplies the quantity as an argument, and zero is rejected at runtime. There is no public initializer that can overwrite the accepted value. This is a useful choice for values whose validity depends on construction-time validation. For models designed for caller-selected object initializers, use required members with validation at the relevant setters or init accessors instead.

Two boundaries to check before choosing a required model

A generic new() factory cannot supply required members

_ = Create<Stock>();
static T Create<T>() where T : new() => new T();
public class Stock
{
    public required int Quantity { get; init; }
}

This produces CS9040. Stock’s implicit public parameterless constructor still requires an initializer, but the generic body cannot supply that contract. Prefer an explicit factory delegate when construction needs data, or redesign the type’s construction API. Do not mark a parameterless constructor as setting required members unless it actually establishes the intended state. See the specification’s new() constraint section.

A struct default bypasses the required-member check

Stock empty = default;
var supplied = new Stock { Quantity = 2 };
Console.WriteLine(empty.Quantity);
Console.WriteLine(supplied.Quantity);
public struct Stock
{
    public required int Quantity { get; init; }
}

Expected and observed output:

0
2

The default value has Quantity zero without any required-member diagnostic. However, this separate program fails with CS9035:

_ = new Stock();
public struct Stock
{
    public required int Quantity { get; init; }
}

The specification’s struct-default section explicitly separates default values from constructor expressions. If zero is invalid for a struct, required members alone cannot eliminate that state. Design code that consumes the struct to handle or reject its default value.

Choose the construction contract your callers need

Use required members when callers should explicitly fill named members. Use a truthful SetsRequiredMembers annotation when a constructor takes over that entire obligation. Use constructor arguments and runtime validation when successful construction must establish a domain rule. Keep assignment requirements, nullable analysis, and runtime validation distinct when reviewing a model.

Continue with the C# inheritance lesson for base-constructor mechanics, or return to the C# learning roadmap.

Share This