init (C# Reference)
In C# 9 and later, the init
keyword defines an accessor method in a property or indexer. An init-only setter assigns a value to the property or the indexer element only during object construction. This enforces immutability, so that once the object is initialized, it can't be changed again.
For more information and examples, see Properties, Auto-Implemented Properties, and Indexers.
The following example defines both a get
and an init
accessor for a property named YearOfBirth
. It uses a private field named _yearOfBirth
to back the property value.
class Person_InitExample
{
private int _yearOfBirth;
public int YearOfBirth
{
get { return _yearOfBirth; }
init { _yearOfBirth = value; }
}
}
Often, the init
accessor consists of a single statement that assigns a value, as it did in the previous example. Note that, because of init
, the following will not work:
var john = new Person_InitExample
{
YearOfBirth = 1984
};
john.YearOfBirth = 1926; //Not allowed, as its value can only be set once in the constructor
The init
accessor can be used as an expression-bodied member. Example:
class Person_InitExampleExpressionBodied
{
private int _yearOfBirth;
public int YearOfBirth
{
get => _yearOfBirth;
init => _yearOfBirth = value;
}
}
The init
accessor can also be used in auto-implemented properties as the following example code demonstrates:
class Person_InitExampleAutoProperty
{
public int YearOfBirth { get; init; }
}
C# language specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
See also
Feedback
Submit and view feedback for