Working With Ref Returns And Ref Local In C# 7.0

I am here to continue the series related to C# 7.0 features. Today we will be going through feature called Ref Returns and Ref Local and will demonstrate its uses in an easy manner.

This article assumes that you have a basic understanding of ref parameter and you want to know the changes introduced in C# 7.0, however, if you are not clear of how ref works, you may follow the article given below for the same.

ref (C# Reference)

Ref Returns

So far ref was only used to be passed as a parameter in a method however there was no provision to return it and use it later. In C# 7.0, this constraint has been waived off and now you can return references as well.

This change is really adding flexibility to handle the scenarios when we want references to return in order to make in-lined replacement.

Ref Local

Ref local is a new variable type introduced in C# 7.0 to store the references. It is mostly used in conjunction with Ref returns to store the reference in a local variable.

Working with Ref Returns and Ref Local

Let’s look at the code to understand how actually Ref Returns and Ref Local works. 

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Console.Title = "RefLocalnReturn Demo";  
  6.             //ref returns and ref local - Before C#7, ref was only used as input params  
  7.             char[] chararray = { 's''a''t''n''a' };  
  8.             Console.WriteLine($"Before ref replacement: {new string(chararray)}");  
  9.             ref char charref = ref RefLocalnReturn.FindCharReference('s', chararray); //charref is a ref local  
  10.             charref = 'p'// replaces s with p in the array  
  11.             Console.WriteLine($"After ref replacement: {new string(chararray)}");  
  12.         }  
  13.     }  
  14.   
  15.   
  16. class RefLocalnReturn  
  17.     {  
  18.         public static ref char FindCharReference(char value, char[] charArray)  
  19.         {  
  20.             for (int i = 0; i < charArray.Length; i++)  
  21.             {  
  22.                 if (charArray[i] == value)  
  23.                 {  
  24.                     return ref charArray[i]; // return the storage location, not the value  
  25.                 }  
  26.             }  
  27.   
  28.             throw new IndexOutOfRangeException(value + "not found");  
  29.         }  
  30.     }   

Output

C#

Now, let’s examine the code closely.

You can see below that ref has been put before the chat type to indicate that it’s going to return the reference of the variable.

  1. public static ref char FindCharReference(char value, char[] charArray)  

Also at the receiving end, we have a ref local variable called charref to hold the reference.

  1. ref char charref = ref RefLocalnReturn.FindCharReference('s', chararray);  

Interesting point on Ref Returns method

Can you think of any method that can be put on the left side of an assignment operator?

Well, you read it right and answer is yes. Ref returns method can be put on the left side to the assignment to avoid the uses of ref local variable.

Let’s look at the code and the output.

  1. //Ref return methods can also be put at left side of assignment.  
  2. RefLocalnReturn.FindCharReference('s', chararray) = 'p';  
  3. Console.WriteLine($"After ref replacement: {new string(chararray)}");  

The code given above is equal to the below (used with ref local)

  1. ref char charref = ref RefLocalnReturn.FindCharReference('s', chararray); //charref is a ref local  
  2. charref = 'p'// replaces s with p in the array  
  3. Console.WriteLine($"After ref replacement: {new string(chararray)}");  

The output remains the same in both the cases.

Restrictions on Ref Returns and Ref Local

Although it’s really nice to work with Ref Returns and Ref Local, however to address the code safely, a few restrictions have been put on them, as shown below.

  • You can’t return reference of anything and everything but to those fields in your objects, which are safe to return.
  • Ref locals can’t be muted or changed to a point to another memory location as they are initialized to a certain memory location.

Conclusion

We have started this article with defining Ref returns and Ref local and then we have seen how these features can be used. We have also gone through the interesting feature of Ref returns, which can be put to left to an assignment operator. In the end, we have gone through some of the restrictions in using them.

You can also download the attached demo project (RefLocalnReturns.zip) to go through the full source code referred in the article.

Hope, you have liked the article. Look forward for your comments/suggestions.

In case, you also want to go through the previous parts of the series, the links are given below.