> ## 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.

# C# Init-Only Property

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.

```csharp theme={"dark"}
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**.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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)

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
