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.
public class Book { public string Title { get; init; } = ""; }
Try it
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.
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
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
requiredto force callers to supply a value. Without it, the property silently keeps its default. - Records. Positional record parameters become
initproperties automatically.withexpressions on records and structs rely on init accessors. - Validation. Write an explicit
initbody 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). Changingsettoinitis 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.Jsonand most mappers can fill init-only properties. - Structs.
initworks on struct properties too; areadonly structrequires properties to be get-only or init-only.
Related
- required
- readonly
- record
- Tutorial: Properties, Records