C# Feature - ref struct
Overview
Section titled “Overview”ref struct defines stack-only types with strong lifetime rules, which is the basis for types such as Span<T>.
Introduced In
Section titled “Introduced In”C# 7.2 (2017)
Example
Section titled “Example”public ref struct ParserState{ public Span<byte> Buffer;}Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Ref structs cannot be boxed, captured by lambdas, or used as fields in reference types.
- Reach for them only when lifetime constraints are a real benefit.