C#泛型比較大小


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 泛型比較大小
{
    class Program
    {
        //Compare<T>是泛型類,T為類型參數
        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;
                }           
            }
        }
        static void Main(string[] args)
        {
            //調用泛型方法
            Console.WriteLine(Compare<int>.compareGeneric(1, 2));
            Console.WriteLine(Compare<string>.compareGeneric("aa", "a"));
            Console.ReadKey();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM