ref Locals and ref Returns · TL;DR
1 min readTL;DR
A ref local (ref int x = ref array[0];) is not a new variable holding a value: it is an alias, a managed pointer to existing storage such as an array element, a field or another variable. Reading or writing x reads or writes that storage directly. A ref return (public ref int Find(...)) lets a method return such a reference instead of a copy of the value, so the caller can read or modify the element in place. Both arrived in C# 7.0; C# 7.2 added ref readonly returns and locals, C# 7.3 allowed reassigning a ref local (x = ref other;) and ref in foreach over spans, and C# 11 added ref fields inside ref structs (which is how Span<T> is built). They exist for performance: working on large structs in arrays without copying them, updating dictionary values in place, and building span-like types. The compiler's escape rules stop you returning a reference to a local that is about to disappear. Traps: forgetting ref at the call site silently gives you a copy, and a reference into a List<T>'s backing array goes stale when the list grows.