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

As a programmer, we are familiar with ‘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 don’t want to go into depth as I am expecting readers have some basic knowledge on ‘ref’ keyword. If you 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 this variable we call as ‘Ref Local’ as it represents a local variable but stores the address location of the returned value. The variable returned, we call it as ‘Ref Return’.

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

public class program 
{ 
    public static void Main(string[] args) 
    { 
        string strStart = "I am learning c# 7.0"; 
        Console.WriteLine("Before: " + strStart); 
 
        ref  string strConclusion = TrainingCSharp7(strStart); 
 
        Console.WriteLine("After: " + strStart); 
 
        strConclusion = "I have started working with c# 7.0"; 
 
        Console.WriteLine("After Modified: " + strStart); 
 
//Using Local Functions feature in C# 7.0 
        public static ref string TrainingCSharp7(string strStart) 
        { 
            strStart.Replace("learning", "learn't"); 
         
            return ref strStart; 
        } 
     
    } 
}

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

//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 some awareness on this feature too. If you like to know more information on Local Functions please visit my other dedicated article on this

Working with Local Functions in C# 7.0 

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 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 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 which replaces below 2 lines 

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 understandable on this topic. Please leave a comment below which help me in writing more articles like this.

 

Happy coding 🙂