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.