Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An init-only property is a property that restricts assignment to the phase of object construction. By replacing the standard set accessor with the init keyword, the C# compiler enforces immutability for that property once the initialization phase completes.
public class ServerConfiguration
{
    public string Hostname { get; init; }
    public int Port { get; init; } = 80; // Inline initialization is permitted
}

The Initialization Phase

The C# compiler defines a strict window during which an init accessor can be invoked. Assignment is exclusively permitted in the following contexts:
  1. Object Initializers: During the instantiation of the object using curly brace syntax.
  2. Constructors: Within the constructor of the declaring type or a derived type.
  3. Inline Initializers: At the point of property declaration.
  4. with Expressions: During non-destructive mutation to create a new instance.
  5. Other init Accessors: Within the body of another init accessor on the same instance.
Once the execution context leaves these boundaries, the property becomes strictly read-only. Attempting to assign a value to an init property outside this phase results in compiler error CS8852.
var config = new ServerConfiguration 
{ 
    Hostname = "localhost" // Valid: Object initializer
};

// config.Hostname = "remote"; // INVALID: Compile-time error CS8852

Explicit Implementation and Readonly Backing Fields

When manually implementing an init accessor rather than using an auto-implemented property, the accessor is granted special compiler privileges to mutate readonly backing fields. This mirrors the behavior of a constructor.
public class TemperatureSensor
{
    private readonly string _sensorId;

    public string SensorId
    {
        get => _sensorId;
        init
        {
            // The init accessor bypasses standard readonly constraints
            // strictly during the initialization phase.
            _sensorId = value ?? throw new ArgumentNullException(nameof(value));
        }
    }
    
    public string CompositeId
    {
        get => $"{SensorId}-01";
        init
        {
            // Valid: Assigning to another init-only property from within an init accessor
            SensorId = value; 
        }
    }
}

Interaction with with Expressions

Init-only properties are foundational to non-destructive mutation in C#. When a with expression is evaluated, the compiler creates a clone of the original object and then executes the init accessors for the properties specified in the initializer block.
public record Point
{
    public int X { get; init; }
    public int Y { get; init; }
}

var p1 = new Point { X = 10, Y = 20 };
var p2 = p1 with { X = 50 }; // Valid: 'with' expression invokes the init accessor for X

Type Compatibility

The init accessor is a feature of the property system itself, not tied to a specific type category. It is fully supported across:
  • class
  • struct
  • record (Positional properties are compiled as get; init; for record class and readonly record struct. However, positional properties in a standard record struct are compiled as get; set;)
  • interface (Interfaces can declare init accessors to enforce initialization contracts on implementing types)
Master C# with Deep Grasping Methodology!Learn More