ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
ref struct is a custom value type in C# that is strictly constrained to be allocated only on the execution stack. The compiler enforces a rigorous set of rules to guarantee that instances of a ref struct can never escape to the managed heap, preventing them from being boxed, captured by closures, or embedded within heap-allocated objects.
struct types can be promoted to the heap (e.g., when boxed, when used as a field in a class, or when captured in an asynchronous state machine), the ref struct modifier was introduced to provide a compiler-enforced guarantee of stack-only semantics.
Compiler Constraints
To maintain the stack-only guarantee, the Roslyn compiler enforces the following structural and behavioral restrictions on anyref struct:
- No Boxing: A
ref structcannot be cast toobject,dynamic, orValueType. - Interface Restrictions: While a
ref structcan implement interfaces (as of C# 13), it cannot be cast or boxed directly to the interface type. It can only participate in interface dispatch through generic methods utilizing theallows ref structanti-constraint. - Field Restrictions: A
ref structcannot be declared as a field within aclassor a standardstruct. It can only be a field within anotherref struct. - No Closures: A
ref structcannot be captured by lambda expressions or local functions. - Asynchronous State Machines: A
ref structcannot be declared as a local variable in anasyncmethod if it is held across anawaitboundary, because the compiler rewritesasyncmethods into state machine objects that reside on the heap. - Iterators: A
ref structcannot be held across ayield returnboundary within an iterator method. As of C# 13, they are permitted as local variables in iterators as long as their lifetime does not span across theyieldstatement, which would require lifting them into a heap-allocated state machine.
ref Fields (C# 11+)
A ref struct is the only type in C# permitted to declare ref fields. A ref field stores a managed pointer (an interior reference) to data, rather than the data itself. This restriction exists for lifetime safety: it prevents heap-allocated objects from holding pointers to stack-allocated variables. If a heap object held a reference to a stack variable, that reference would become a dangling pointer as soon as the stack frame unwinds.
Generic Anti-Constraints (C# 13+)
Historically, aref struct could not be used as a generic type argument because generic types do not guarantee stack-only allocation. Starting in C# 13, a ref struct can be passed as a generic type argument if the generic parameter explicitly declares the allows ref struct anti-constraint. This forces the generic type or method itself to adopt ref struct allocation rules.
readonly ref struct
A ref struct can be combined with the readonly modifier. This enforces immutability on the struct’s fields while maintaining the stack-only allocation constraints.
In a readonly ref struct, all standard fields must be marked readonly. For ref fields, the field must be declared with the readonly modifier (readonly ref T). This ensures the reference itself cannot be reassigned to point to a different memory location. It does not require the referent (the underlying data) to be immutable, though you can optionally add readonly to the referent type (readonly ref readonly T) if strict deep immutability is required.
Master C# with Deep Grasping Methodology!Learn More





