Skip to content

C# Feature - ref locals

Ref locals let a local variable alias another storage location, which avoids copying and can allow in-place updates.

C# 7.0 (2017)

var item = values[0];
item++;
ref int item = ref values[0];
item++;
  • Use ref locals only when aliasing is intentional and clearly beneficial.
  • Overuse can make code harder to reason about than ordinary value copies.