Skip to content

C# Feature - ref struct

ref struct defines stack-only types with strong lifetime rules, which is the basis for types such as Span<T>.

C# 7.2 (2017)

public ref struct ParserState
{
public Span<byte> Buffer;
}
  • 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.