Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Ref locals and Ref returns in C# 7.0?
C# 7.0 introduced ref locals and ref returns, which allow methods to return references to variables instead of copies of their values. This enables direct modification of the original data through the returned reference.
A ref return allows a method to return a reference to a variable, and the caller can create a ref local variable that directly references the returned memory location.
Syntax
Following is the syntax for declaring a ref return method −
public static ref ReturnType MethodName(parameters) {
return ref variable;
}
Following is the syntax for creating a ref local variable −
ref ReturnType localVar = ref MethodName(parameters);
Regular Variable Assignment (No Reference)
In this example, modifying the color variable doesn't affect the original array because it's a copy −
Example
using System;
class Program {
public static void Main() {
var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
string color = colors[3];
color = "Magenta";
Console.WriteLine(String.Join(" ", colors));
}
}
The output of the above code is −
blue green yellow orange pink
Using Ref Locals
With ref locals, the variable directly references the array element, so modifications affect the original array −
Example
using System;
class Program {
public static void Main() {
var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
ref string color = ref colors[3];
color = "Magenta";
Console.WriteLine(String.Join(" ", colors));
}
}
The output of the above code is −
blue green yellow Magenta pink
Regular Method Return (No Reference)
When a method returns a regular value, modifications don't affect the original data −
Example
using System;
class Program {
public static void Main() {
var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
string color = GetColor(colors, 3);
color = "Magenta";
Console.WriteLine(String.Join(" ", colors));
}
public static string GetColor(string[] col, int index) {
return col[index];
}
}
The output of the above code is −
blue green yellow orange pink
Using Ref Returns
With ref returns, the method returns a reference to the original array element −
Example
using System;
class Program {
public static void Main() {
var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
ref string color = ref GetColor(colors, 3);
color = "Magenta";
Console.WriteLine(String.Join(" ", colors));
}
public static ref string GetColor(string[] col, int index) {
return ref col[index];
}
}
The output of the above code is −
blue green yellow Magenta pink
Key Rules
-
The
refkeyword must be used both in the method signature and at the return statement. -
The caller must use
refwhen assigning to a ref local variable. -
The referenced variable must have a lifetime that extends beyond the method call.
-
Ref returns cannot return local variables or constants.
Conclusion
Ref locals and ref returns in C# 7.0 provide direct access to memory locations, allowing efficient modification of original data without copying values. They are particularly useful for performance-critical scenarios where avoiding unnecessary copies is important.
