Ref Return and Ref Local In C#


This article intends to demonstrate new features Ref Return and Ref Local In C# 7.0. The ref is used to be passed as a argument in the method. It enhanced with return and uses returned value later in the method using ref return and ref local.

  1. Ref: It uses to pass an argument to a method by reference. It uses in a method call and signature.
  2. Ref Return: It uses to return a value to the caller by reference. It uses in a method body and signature.
  3. Ref Local: It uses to store a value locally as a reference that caller intends to modify.
You may like to read

  1. Pattern Matching In C#
  2. Expression Bodied Members In C#

Working with Ref


The ref keyword uses to pass an argument to a method by reference. The reference argument is a reference to a memory location of a variable. There are some more points.

  1. When an argument passes with ref keyword in the method then method works with same variable value that is passed in the method call. If variable value changes then function argument value also changes.
  2. Both the method definition and calling must explicitly use the ref keyword.
  3. A ref parameter must be initialized first before it passes in the method.
  4. A ref parameter must not be declared as a constant variable.
  5. It is not obligatory that ref argument name should be same in both method signature and method call.

Let’s see an example. In this example, we create a program to swap to integer number without using the third variable.

As per above code snippet, arguments pass by reference. It shows that the values have changed inside the swap function and this change reflects in the Main function.

Working with Ref Return and Ref Local


Ref Return uses to return by reference. We add the keyword ref before the return type of any method signature and use ref keyword with return in the method body. It improves performance as it doesn’t require assign new memory location and copy resources.

Ref Local uses to store a reference into a local variable. We define the local variable as a reference by adding the keyword ref before the variable type and add the keyword ref before the method call.

Let’s see an example. We define a class named Student as per following code snippet.

Now we define entry point program of the application. This program uses both ref return and ref local. The following code snippet for same.

Now runs the application and shows the result as par following figure.


Output 1: ref return and ref local

As per above code, we do the addition of values in marks array and show the result. We create a method named MarkAddtion that returns by reference. It returns the last item of input array marks with reference rather than its value. This method MarkAddtion calls by reference and assigns return reference to a local reference variable. Now local reference variable values updates with a new value, and thus the value of the last item of the array also updates. We do the sum of values in marks array again and see that sum with a new value.

A method can’t return a local int variable by reference but the method can return an array of int by reference. The int is a value type, and thus the variable gets out of scope at the end of the method, and thus a reference to it cannot be returned. An array is a reference type, and the array is allocated on the heap. An int within the array can be returned with the ref keyword. It is also possible with a parameter that is passed by reference.

Conclusion


This feature is introduced to improve performance. It uses in an algorithm that requires large amounts of memory for the different structure.