ZetCode

C# required modifier

last modified October 5, 2026

This article shows how to declare required members in C#. The required modifier was introduced in C# 11 and lets a type state which members callers must initialize when creating an object.

C# required property example

A required property must be assigned by an object initializer or by a constructor annotated with [SetsRequiredMembers]. The compiler reports an error when construction leaves a required member unassigned.

Program.cs
var book = new Book
{
    Title = "The C# Guide",
    Author = "Sam Lee"
};

Console.WriteLine($"{book.Title} by {book.Author}");

class Book
{
    public required string Title { get; init; }
    public required string Author { get; init; }
}
$ dotnet run
The C# Guide by Sam Lee

Both properties are required, so object creation must provide values for both. Required does not mean non-null: nullable annotations and validation are separate concerns.

Required fields and properties

The modifier can be used on fields and properties, including properties with setters. Required members are a compile-time construction rule, not a guarantee that the value can never change afterward.

Program.cs
var point = new Point { X = 3, Y = 7 };
Console.WriteLine($"({point.X}, {point.Y})");

class Point
{
    public required int X;
    public required int Y { get; set; }
}
$ dotnet run
(3, 7)

Constructors and required members

A constructor can declare that it sets all required members by using SetsRequiredMembersAttribute. This attribute is a promise to the compiler; it does not verify the constructor's implementation.

Program.cs
using System.Diagnostics.CodeAnalysis;

var user = new User("Mina");
Console.WriteLine(user.Name);

class User
{
    public required string Name { get; init; }

    [SetsRequiredMembers]
    public User(string name)
    {
        Name = name;
    }
}
$ dotnet run
Mina

The attribute applies to the constructor and its callers do not need to initialize the required member separately. Use it only when the constructor actually fulfills that contract.

Required member rules

Source

The required modifier - C# reference

This article introduced the required modifier in C#.

Author

Jan Bodnar writes programming tutorials and educational content.

List all C# tutorials.