CSharp - Method ref Returns

Introduction

You can return a ref local from a method. This is called a ref return.

Demo

using System;
class MainClass//w  ww  . j av  a2 s .c  o  m
{
     static string X = "Old Value";

     static ref string GetX() {
        return ref X;    // This method returns a ref
     }

   public static void Main(string[] args)
   {

       ref string xRef = ref GetX();       // Assign result to a ref local
       xRef = "New Value";
       Console.WriteLine (X);              // New Value

   }
}

Result