New C# 7 Features - Ref Returns And Out Variables - Part Three

In C# 7.0, lots of enhancements and new features have been introduced with Visual Studio 2017 which makes developers' lives easy. Let's explore two of these features, like ref return and out variable, with a small example which will help us to understand easily.

This is part three of C# 7.0, If you want to read about more feature then visit the below links for other parts,
Ref Returns

In C#7, the Ref keyword allows code to pass a value type variable by reference as a parameter of a method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword. Here, I have chosen FindFruits() to demonstrate the ref returns feature. FindFruits() method has a two parameter index, a list of fruits and it has a ref string return type, this method returns fruit names based on the passed index.

Initially, I have passed position = 2, so this method will return output as "Papaya" from the list then I assigned "Watermelon" to secondFruit ref variable. Now, if I try to pass the same position in this list then I will get newly replaced fruit from the list.

Ref keyword that can be used to return values by a ref. Line 17 created a method with return type ref string, Ref can also be used for storing values by a ref in the local variable. Line 12 stored values in secondFruit ref variable,
  1. class Program      
  2.     {      
  3.         static void Main(string[] args)      
  4.         {      
  5.             string[] listOfFruits = { "Mango""Banana""Papaiya""Chiku""MuskMelon"};      
  6.       
  7.             int position = 2;      
  8.       
  9.             ref string secondFruit = ref FindFruits(position, listOfFruits);      
  10.             Console.WriteLine($"Fruit Number Two : {secondFruit}");      
  11.       
  12.             secondFruit = "Watermelon";      
  13.             Console.WriteLine($"Second fruits is replaced with {listOfFruits[position]}");      
  14.             Console.ReadLine();      
  15.       
  16.       
  17.             ref string FindFruits(int index, string[] fruitList)      
  18.             {      
  19.                 if (fruitList.Length > 0)      
  20.                     return ref fruitList[index]; // this return the location not the value      
  21.                 throw new IndexOutOfRangeException($"{nameof(index)} not found");      
  22.       
  23.             }      
  24.         }      
  25.     }      
 C#
 
Out Variables

Before C# 7.0 if we want to use out variable then we have to declare that variable before passing as a parameter in the method and we were not able to use datatype while passing it as a parameter. Here is the example of the old way of using out variable.  
  1. class Program      
  2.     {      
  3.         static void Main(string[] args)      
  4.         {      
  5.             string firstName;      
  6.             string lastName;      
  7.       
  8.             CreateName(out firstName, out lastName);      
  9.             Console.WriteLine($"Full Name is {firstName} { lastName}");      
  10.             Console.ReadLine();      
  11.       
  12.         }      
  13.         private static void CreateName(out string firstName, out string lastName)      
  14.         {      
  15.             firstName = "Jignesh";      
  16.             lastName = "Kumar";      
  17.         }      
  18.     }      

In C# 7.0, it's not mandatory to declare a variable before it passes as a parameter. Now, it allows us to declare variables while passing to the parameter. Look at the below example and the new way of passing out variable in C# 7.0, we don't need to specify the type of variable and we can use these two parameters as out parameter like out var firstName and out var lastName.

  1. class Program      
  2.     {      
  3.         static void Main(string[] args)      
  4.         {      
  5.             //string firstName;      
  6.             //string lastName;      
  7.       
  8.             CreateName(out var firstName, out var lastName);      
  9.             Console.WriteLine($"Full Name is {firstName} { lastName}");      
  10.             Console.ReadLine();      
  11.       
  12.         }      
  13.         private static void CreateName(out string firstName, out string lastName)      
  14.         {      
  15.             firstName = "Jignesh";      
  16.             lastName = "Kumar";      
  17.         }      
  18.     }      

Thank you for reading... happy coding.

I hope the above blog is useful to you. 


Similar Articles