This is part three of C# 7.0, If you want to read about more feature then visit the below links for other parts,
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,
- class Program
- {
- static void Main(string[] args)
- {
- string[] listOfFruits = { "Mango", "Banana", "Papaiya", "Chiku", "MuskMelon"};
- int position = 2;
- ref string secondFruit = ref FindFruits(position, listOfFruits);
- Console.WriteLine($"Fruit Number Two : {secondFruit}");
- secondFruit = "Watermelon";
- Console.WriteLine($"Second fruits is replaced with {listOfFruits[position]}");
- Console.ReadLine();
- ref string FindFruits(int index, string[] fruitList)
- {
- if (fruitList.Length > 0)
- return ref fruitList[index]; // this return the location not the value
- throw new IndexOutOfRangeException($"{nameof(index)} not found");
- }
- }
- }

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.
- class Program
- {
- static void Main(string[] args)
- {
- string firstName;
- string lastName;
- CreateName(out firstName, out lastName);
- Console.WriteLine($"Full Name is {firstName} { lastName}");
- Console.ReadLine();
- }
- private static void CreateName(out string firstName, out string lastName)
- {
- firstName = "Jignesh";
- lastName = "Kumar";
- }
- }
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.
- class Program
- {
- static void Main(string[] args)
- {
- //string firstName;
- //string lastName;
- CreateName(out var firstName, out var lastName);
- Console.WriteLine($"Full Name is {firstName} { lastName}");
- Console.ReadLine();
- }
- private static void CreateName(out string firstName, out string lastName)
- {
- firstName = "Jignesh";
- lastName = "Kumar";
- }
- }
Thank you for reading... happy coding.
I hope the above blog is useful to you.


Murali KrishnanPosted Sep 4, 2018, 9:51 PM EDT
Thanks Jignesh :) on behalf of MC05
Mahesh ChandTOP 10Posted Jul 30, 2018, 5:39 AM EDT
Now, you can add "in" modifier as well. It's ref, out, and in now in C# 7.2.