CSharp - Use ref modifier to implement a swap method

Introduction

You can use ref modifier to implement a swap method.

Demo

using System;
class Test//from  w  w  w.java2 s.c o  m
{
       static void Swap (ref string a, ref string b)
       {
         string temp = a;
         a = b;
         b = temp;
       }
       static void Main()
       {
         string x = "A";
         string y = "book2s.com";
         Console.WriteLine (x);
         Console.WriteLine (y);

         Swap (ref x, ref y);
         Console.WriteLine (x);
         Console.WriteLine (y);
       }
}

Result

Related Topic