Select Page

Use init when callers should choose a property’s value while creating an object and ordinary caller code should not replace it afterward. It works on classes as well as records. It does not require a value or freeze the objects a property references.

Examples compiled and ran with .NET SDK 10.0.401, net10.0 and C# 14, with nullable analysis and implicit usings enabled. Each example is a separate console program; statements precede type declarations. The features used here need no C# 14 syntax.

Initialize once, then read

var settings = new Settings { Region = "west" };
Console.WriteLine(settings.Region);
Console.WriteLine(settings.Retries);
settings.Labels.Add("urgent");
Console.WriteLine(settings.Labels.Count);
Console.WriteLine(new Settings().Region == "");
public class Settings
{
    public string Region { get; init; } = "";
    public int Retries { get; init; } = 3;
    public List<string> Labels { get; init; } = new();
}

Recorded output:

west
3
1
True

The object initializer sets Region. Retries keeps its default. Adding a label succeeds because the list remains mutable. Creating Settings without an initializer also succeeds: init controls assignment timing, not whether initialization is mandatory.

What the compiler rejects

Compile this as a separate program:

var item = new Item { Name = "one" };
item.Name = "two";
public class Item { public string Name { get; init; } = ""; }

The recorded compiler diagnostic is CS8852: assigning Name after construction is outside an allowed initialization context. An init accessor can also be assigned within the declaring type’s instance constructor; it is not limited to object initializer syntax.

Require a caller-supplied value

var item = new Item { Name = "Notebook" };
Console.WriteLine(item.Name);
var original = new Quote("Notebook", 5);
var revised = original with { Count = 8 };
Console.WriteLine(original.Count);
Console.WriteLine(revised.Count);
public class Item
{
    public required string Name { get; init; }
}
public record Quote(string Name, int Count);

Recorded output:

Notebook
5
8

required adds a separate obligation. This example needs C# 11 or later because it uses required members; init-only accessors and record classes were introduced in C# 9.

var item = new Item();
public class Item { public required string Name { get; init; } }

This separate negative example produces CS9035 because Name was omitted. Required is a compiler initialization check, not runtime input validation: it does not guarantee a nonempty string. The nullable analysis and validation rules you choose still matter.

Validate in an init accessor

var valid = new Price { Amount = 12m };
Console.WriteLine(valid.Amount);
try { _ = new Price { Amount = -1m }; }
catch (ArgumentOutOfRangeException error)
{
    Console.WriteLine(error.ParamName);
}
public class Price
{
    private decimal amount;
    public decimal Amount
    {
        get => amount;
        init => amount = value >= 0 ? value
            : throw new ArgumentOutOfRangeException(nameof(Amount));
    }
}

Recorded output:

12
Amount

An explicit backing field keeps this pattern usable without the newer field keyword. The accessor rejects a negative supplied amount. Because Amount is not required and defaults to zero, omitting it still creates a valid zero-price object under this rule.

Choose the right kind of immutability

A get-only property suits values assigned internally, including by a constructor. An init-only property additionally lets callers supply a value in an object initializer. A private setter lets code inside the type change the property later.

For a model that owns a collection, decide who may mutate its contents and whether incoming data must be copied. Exposing a read-only view alone does not stop another owner from changing the underlying collection. Keep mutable state behind a deliberate API if changes are part of the model.

A record’s with expression creates a copy with selected values changed, as Quote demonstrates. Its referenced objects are shared unless you explicitly replace them. Learn more through the C# learning guide.

Sources

See Microsoft’s init reference, required reference and record reference. Sources checked October 15, 2026.

Share This