Swapping via Reference (Generic Method)

CSharp · Console

/Program.cs

using System;

public class Maths {

  public static void Swap<T>(ref T a, ref T b) {
    T temp = a;
    a = b;
    b = temp;
  }
}

public class Program {
  public static void Main() {
    int twenty = 20;
    int thirty = 30;
    string forty = "forty";
    string fifty = "fifty";
	
	Maths.Swap<int>(ref twenty, ref thirty);
    Console.WriteLine("twenty = {0}", twenty);
    Console.WriteLine("thirty = {0}", thirty);

    Maths.Swap<string>(ref forty, ref fifty);
    Console.WriteLine("forty = {0}", forty);
    Console.WriteLine("fifty = {0}", fifty);

  }
}
;