C# Feature - ref returns
Overview
Section titled “Overview”Ref returns allow a method to return an alias to existing storage instead of a copied value.
Introduced In
Section titled “Introduced In”C# 7.0 (2017)
Example
Section titled “Example”static ref int Find(int[] data) => ref data[0];
ref int first = ref Find(values);first++;Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Only return references to storage that outlives the method call.
- Avoid ref returns unless mutation of the original storage is the goal.