1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 13 //传入int类型OK 14 int a, b; 15 a = 2; 16 b = 3; 17 swap<int>(ref a, ref b); 18 Console.WriteLine("a(2):" + a + ",b(3):" + b); 19 Console.ReadKey(); 20 21 //传入string类型OK 22 /* 23 string a, b; 24 a = "a"; 25 b = "b"; 26 swap<string>(ref a, ref b); 27 Console.WriteLine("a(a):" + a + ",b(b):" + b); 28 Console.ReadKey(); 29 */ 30 /* 31 string a; 32 int b;//b定义为int,第34行报错。 33 a = "a"; 34 b = 3; 35 swap<string>(ref a, ref b); 36 Console.WriteLine("a(a):" + a + ",b(b):" + b); 37 Console.ReadKey(); 38 */ 39 40 } 41 static void swap<T>(ref T a, ref T b) 42 { 43 T temp = a; 44 a = b; 45 b = temp; 46 } 47 } 48 }
C# 泛型方法
猜你喜欢
转载自www.cnblogs.com/firstzky/p/9176455.html
今日推荐
周排行