ZetCode

C# init members

last modified October 5, 2026

This article explains init accessors in C#. Introduced in C# 9, an init accessor allows a property to be assigned during object initialization while preventing ordinary assignments after construction.

C# init property example

An init-only property can be set in an object initializer. Later assignments from regular code are rejected by the compiler.

Program.cs
var config = new AppConfig
{
    AppName = "Reader",
    Version = 2
};

Console.WriteLine($"{config.AppName} {config.Version}");

class AppConfig
{
    public string AppName { get; init; } = "";
    public int Version { get; init; }
}
$ dotnet run
Reader 2

After creation, config.AppName = "Writer" is not allowed. The init accessor is like a setter restricted to the initialization phase.

Init accessors and constructors

A constructor can also assign an init-only property. This lets a type expose values that callers can configure at construction time but cannot later replace through the property.

Program.cs
var person = new Person("Asha");
Console.WriteLine(person.Name);

class Person
{
    public string Name { get; init; }

    public Person(string name)
    {
        Name = name;
    }
}
$ dotnet run
Asha

Init-only properties and with expressions

Init-only properties are useful with records and non-destructive mutation. A with expression creates a copy with selected values changed, rather than changing the original object.

Program.cs
var first = new Book("Dune", 1965);
var revised = first with { Year = 2021 };

Console.WriteLine($"{first.Title}: {first.Year}");
Console.WriteLine($"{revised.Title}: {revised.Year}");

record Book(string Title, int Year);
$ dotnet run
Dune: 1965
Dune: 2021

The positional record properties support initialization with a with expression. The original value remains unchanged.

Init accessor details

Source

The init keyword - C# reference

This article introduced init-only members in C#.

Author

Jan Bodnar writes programming tutorials and educational content.

List all C# tutorials.