Ref struct, ref readonly struct, ref returning, ref everything
summary
Explains concrete C# features and runtime changes: how ref returns and ref locals avoid copying, how the GC and JIT handle interior pointers, and how C# 7.0–7.3 adds ref ternary, ref foreach and readonly refs that sometimes force defensive copies unless the type is a readonly struct. Shows a working stack-allocated string builder (a ValueStringBuilder) that uses Span<T>, stackalloc and ArrayPool to reduce allocations and improve throughput in hot paths.
AI-written summary · about
chapters (17)
- 0:00Introduction and motivation
- 1:50Class vs struct differences
- 5:02Managed pointers and ref parameters
- 7:59Ref locals and their use cases
- 9:46Ref returning and lifetime rules
- 15:54Ref returning in collections
- 16:58Ref ternary expressions
- 18:04Ref foreach and enumerators
- 19:07Read-only ref semantics
- 21:39Read-only structs and defensive copies
- 27:42Ref struct introduction
- 29:51Advantages of ref structs
- 30:15Disposable ref structs
- 31:22Span and managed pointers
- 34:10Summary of ref features
- 34:51Value StringBuilder demo
- 39:29Q&A session
