Glossary / init properties

init properties

An init accessor permits assignment during object initialization and prevents ordinary reassignment afterward. It supports immutable-style construction and works with object initializers, constructors, and record with expressions, but it does not make the containing object or referenced values deeply immutable.

Syntax and assignment contexts

public sealed class RequestOptions
{
    public string Endpoint { get; init; } = "";
}
var options = new RequestOptions { Endpoint = "/api" };

An init-only property can be assigned in an object initializer, with initializer, instance constructor, or another permitted initialization context. The accessor can validate values, and init-only assignment can interact with readonly fields on the declaring type. After construction, callers cannot normally assign to it. Reflection and unsafe mechanisms lie outside this source-level guarantee.

An init property of reference type can point to a mutable list; callers can mutate the list even though they cannot replace the property. Use immutable collections, defensive copies, or read-only views when deep immutability is required. Nullability and required are separate: init does not ensure a non-null value unless other contracts establish it.

API evolution

Changing set to init may break callers that mutate after construction. New APIs can use init-only properties for configuration, but object initialization still needs validation when values must satisfy cross-property invariants.

Interview questions

  1. When can init-only properties be assigned? During object initialization, constructors, and permitted initialization contexts such as with.
  2. Does init make nested objects immutable? No.
  3. How does init differ from required? Init restricts assignment timing; required demands initialization at creation sites.
  4. Can init contain validation? Yes, it is an accessor.
  5. Does changing set to init preserve compatibility? It can break later assignments by callers.

References