Working With New Enhanced Feature Of ‘Ref’ Keyword In C# 7.0

Here, in this article, I will try to explore the new enhanced feature of ‘ref’ keyword.

As a programmer, we are familiar with the ‘ref’ keyword which allows us to pass a variable location as a method parameter to modify the data in this location address which takes effect after returning back from this method.

Well, I do not want to go into depth as I am expecting readers have some basic knowledge of ‘ref’ keyword. If you are new to this, please refer here.

From C# 7.0, you are allowed to return this ‘ref’ variable and also it allows the user to assign this returned ‘ref’ value to the new variable, and we call this variable ‘Ref Local’ as it represents a local variable but stores the address location of the returned value. The variable returned is called as ‘Ref Return’.

Let’s look the same in the following program and understand this more practically.

  1. public class program   
  2. {   
  3.     public static void Main(string[] args)   
  4.     {   
  5.         string strStart = "I am learning c# 7.0";   
  6.         Console.WriteLine("Before: " + strStart);   
  7.    
  8.         ref  string strConclusion = TrainingCSharp7(strStart);   
  9.    
  10.         Console.WriteLine("After: " + strStart);   
  11.    
  12.         strConclusion = "I have started working with c# 7.0";   
  13.    
  14.         Console.WriteLine("After Modified: " + strStart);   
  15.    
  16. //Using Local Functions feature in C# 7.0   
  17.         public static ref string TrainingCSharp7(string strStart)   
  18.         {   
  19.             strStart.Replace("learning""learn't");   
  20.            
  21.             return ref strStart;   
  22.         }   
  23.        
  24.     }   
  25. }  

When we run the above program, the following is the output.

  1. //Output Before: I am learning c# 7.0 After: I am learn't c# 7.0 After Modified: I have started working with c# 7.0  

Let’s understand what we did in the above program. Before this, I used another new feature, Local Functions in C# 7.0, in this program to make you aware of this feature too. If you'd like to know more information on Local Functions please visit my other dedicated article on this

In the above program, we are trying to modify the string in the method TrainingCSharp7 by passing this string as a parameter.

Note
Earlier we need to decorate this parameter with ref keyword, but now this is not needed.

Here in this local method, we are trying to replace the word ‘learning’ to “learn’t” and returning the modified string. As observed while this is returning we used a ‘ref’ keyword, which gives the instruction to the system to send the location address of this string instead of value string. We call this style of returning as ‘ref return‘.

Once this method returns the address location, we are trying to assign to a variable ‘strconclusion’. As observed, this variable is prefixed with ‘ref’ keyword, it represents ‘ref Local‘ and this variable will store the address location rather than storing the string value. It means if we assign any other string to this variable it will override the string in strstart variable as we can see in the above program where we are replacing with the string “I have started working with c# 7.0“.

One more interesting statement below replaces the below two lines from:

ref string strConclusion = TrainingCSharp7(strStart); strConclusion = "I have started working with c# 7.0";

To:

TrainingCSharp7(strStart) = "I have started working with c# 7.0";

There are some limitations to using this feature, those are:

  • We cannot return any unsafe objects.
  • These local variables cannot be assigned to any other address locations.

Hope this article make you understand this topic. Please leave a comment below which will help me in writing more articles like this.

Happy coding...


Recommended Free Ebook
Similar Articles