Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The init accessor

Reference for the C# init accessor: init-only properties, immutability after construction, object initializers, with expressions and how it differs from set.

init is a property accessor, used in place of set, that allows assignment only while an object is being created. After construction finishes, the property is read-only. This gives you immutable objects that can still be created with the readable object initializer syntax new T { A = 1, B = 2 }.

C# 9 introduced it for exactly that gap: a property with only get can be set only from a constructor, and a property with set can be changed forever. init sits between the two.

C#
public class Book
{
    public string Title { get; init; } = "";
}

Try it

C# Set during creation, frozen afterwards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# init with validation and with-expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public Type Name { get; init; }
public Type Name { get => field; init => field = Check(value); }   // C# 14 field keyword
public Type Name { get; init; } = defaultValue;
public Type this[int i] { get { ... } init { ... } }
Part Description
init An accessor that behaves like set but is callable only during initialization: in an object initializer, a with expression, an attribute, or on this/base inside a constructor or another init accessor.
Value The implicit parameter value, as in set.
Accessibility The accessor can be given a narrower modifier, for example public int X { get; protected init; }.

Errors: assigning outside those contexts is CS8852. A property cannot have both set and init; the second accessor is rejected as already defined (CS1007).

Usage notes

  • Shallow immutability. An init-only property holding a List<T> can still have items added to the list. Only the reference is fixed.
  • Combine with required to force callers to supply a value. Without it, the property silently keeps its default.
  • Records. Positional record parameters become init properties automatically. with expressions on records and structs rely on init accessors.
  • Validation. Write an explicit init body to check or normalize the value, as in the second example. Validation of relations between properties needs a constructor, because init accessors run one at a time in the initializer's order.
  • Order. Init accessors run after the constructor body in the order written in the initializer. Do not rely on a property being set when another's accessor runs.
  • Binary compatibility. The modifier is encoded as modreq(IsExternalInit). Changing set to init is a breaking change for callers that assign after construction.
  • Targets older than .NET 5 need a polyfill declaring System.Runtime.CompilerServices.IsExternalInit.
  • Serialization. System.Text.Json and most mappers can fill init-only properties.
  • Structs. init works on struct properties too; a readonly struct requires properties to be get-only or init-only.