The ref modifier is essential in implementing a swap method - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Description

The ref modifier is essential in implementing a swap method

Demo Code

using System;/*from w  w w .  j a  v a  2 s .c  o  m*/
class Test
{
   static void Swap (ref string a, ref string b)
   {
      string temp = a;
      a = b;
      b = temp;
   }
   static void Main()
   {
      string x = "Penn";
      string y = "Teller";
      Swap (ref x, ref y);
      Console.WriteLine (x);   // Teller
      Console.WriteLine (y);   // Penn
   }
}

Result


Related Tutorials