C# 泛型 Generic

C# 泛型 Generic_第1张图片

Demo 

using System;

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

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

 

你可能感兴趣的:(C#)