范型经典案例

一个交换两个值得函数,

static void Main(string[] args)
        { 
string n1 = "ni1",n2="mi2";             swap<string>(ref n1, ref n2);             Int32 m1 = 111, m2 = 222;             swap<Int32>(ref m1, ref m2);             Console.ReadKey();         }         private static void swap<T>(ref T o1,ref T o2)         {             T temp;             temp = o1;             o1 = o2;             o2 = temp;             Console.WriteLine("o1:"+o1+"o2:"+o2);         }
一个方法,能够用于不同类型的参数,避免不必要的装箱。
原文地址:https://www.cnblogs.com/fjsnail/p/3254848.html