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.