C# 泛型 Generic

Demo 

using System;

namespace GenericDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Compare<int>.CompareGeneric(3, 4));
            Console.WriteLine(Compare<string>.CompareGeneric("abc", "d"));
            Console.ReadLine();
        }
    }

    public class Compare<T> where T : IComparable
    {
        public static T CompareGeneric(T t1,T t2)
        {
            if (t1.CompareTo(t2) > 0)
            {
                return t1;
            }
            else
            {
                return t2;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u012664198/article/details/82840657