自定义泛型方法

泛型的接口主要有:ICollection<T>,IComparer<T>,IDictionary<T>,IEnumerable<T>,IEnumerator<T>,IList<T>;

已经实现的类有:Collection<T>,Comparer<T>, Dictionary<T>, List<T>, Queue<T>, SortedDictionary<T>, Stack<T>,LinkedList<T>,ReadOnlyCollection<T>

但是除了这些方法外,还可以自定义方法哦。

 public static void Swap<T>(ref T a, ref T b)

        {

            T temp;

            temp = a;

            a = b;

            b = temp;

        }



        static void Main(string[] args)

        {

            int a = 3;

            int b = 4;

            Swap<int>(ref a,ref b);

            Console.WriteLine(a.ToString() + "," + b.ToString());





            string s1 = "hello";

            string s2 = "world";

            Swap<string>(ref s1,ref  s2);

            Console.WriteLine(s1 + s2);

        }

        

你可能感兴趣的:(String)