Skip to content

C# Feature - ref returns

Ref returns allow a method to return an alias to existing storage instead of a copied value.

C# 7.0 (2017)

static ref int Find(int[] data) => ref data[0];
ref int first = ref Find(values);
first++;
  • Only return references to storage that outlives the method call.
  • Avoid ref returns unless mutation of the original storage is the goal.