> ## 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# Readonly Struct

A `readonly struct` is a value type declared with the `readonly` modifier, which enforces strict immutability at the compiler level. When applied to a `struct`, the C# compiler guarantees that the state of the object cannot be modified after initialization, treating the hidden `this` reference as an `in` parameter for all instance members.

```csharp theme={"dark"}
public readonly struct Vector2D
{
    // Fields must be explicitly marked readonly
    public readonly double X;
    
    // Auto-properties must be get-only or init-only
    public double Y { get; init; }

    public Vector2D(double x, double y)
    {
        X = x;
        Y = y;
    }
}
```

## Compiler Rules and Constraints

Applying the `readonly` modifier to a struct imposes the following strict compilation rules:

1. **Instance Fields:** Every instance field declared within the struct must be explicitly marked with the `readonly` keyword.
2. **Properties:** Auto-implemented properties cannot have `set` accessors. They must be get-only or utilize `init` accessors.
3. **Constructors:** As of C# 11, struct constructors no longer require explicit definite assignment of all fields before control leaves the constructor. The compiler automatically initializes any unassigned fields to their default values.
4. **Events:** Field-like events are prohibited because the compiler automatically generates a mutable backing delegate field for them. To implement events in a `readonly struct`, you must explicitly define custom `add` and `remove` accessors.
5. **Static Members:** The `readonly` constraint applies exclusively to instance state. Static fields and static properties within a `readonly struct` are not required to be readonly.

## Mechanics of the `this` Reference

In a standard, non-readonly `struct`, the compiler passes the `this` reference to instance methods and property accessors as a `ref` parameter. This allows instance methods to mutate the struct's internal state.

In a `readonly struct`, the compiler alters the method signature of all instance members to pass the `this` reference as an `in` parameter (`ref readonly`).

```csharp theme={"dark"}
public readonly struct Matrix
{
    public readonly int Value;

    public Matrix(int value) => Value = value;

    // The compiler treats 'this' as an 'in Matrix' parameter here
    public int Compute() => Value * 2; 
}
```

Because `this` is an `in` parameter, the compiler statically guarantees that no instance method can mutate the struct. Consequently, if a `readonly struct` is passed to another method via the `in` modifier, the compiler will not generate hidden defensive copies when invoking its instance members, as the immutability is already guaranteed at the type level.

## Compilation Failures

Attempting to violate the immutability contract results in immediate compiler errors:

```csharp theme={"dark"}
public readonly struct InvalidStruct
{
    // Error CS8340: Instance fields of a readonly struct must be readonly.
    public int MutableField; 

    // Error CS8341: Auto-implemented instance properties in a readonly struct must be readonly.
    public int MutableProperty { get; set; } 

    public readonly int State;

    public InvalidStruct(int state)
    {
        State = state;
    }

    public void AttemptMutation()
    {
        // Error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer).
        State = 42; 
    }
}
```

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