各種泛型大全詳解

什麼是泛型

泛型就是一種不確定的數據類型。

泛型的優點

  1. 避免拆箱裝箱,提升性能
  2. 可以避免運行時轉換類型出現的錯誤

泛型方法、泛型約束

 

Program.cs類:


    class Program
    {
        static void Main(string[] args)
        {
            int iValue = 123;
            string sValue = "456";
            DateTime dtValue = DateTime.Now;
            object oValue = "789";

            //泛型方法
            CommonMethod.Show<int>(iValue);
            CommonMethod.Show<string>(sValue);
            CommonMethod.Show<DateTime>(dtValue);
            CommonMethod.Show<object>(oValue);
            Chinese chinese = new Chinese()
            {
                Id = 001,
                Name = "趙日天",
                Country = "中國",
            };
            Japanese japanese = new Japanese()
            {
                Id = 002,
                Name = "山本",
                Country = "日本",
            };
            CommonMethod.Introduce<People>(chinese);
            CommonMethod.Introduce<People>(japanese);
        }

    }

CommonMethod.cs類:


    public class CommonMethod
    {
        public static void Show<T>(T tParameter)
        {
            Console.WriteLine("類型是:{0},值是:{1}",tParameter.GetType().Name, tParameter);
        }

        public static void Introduce<T>(T tParameter) where T : People
        {
            Console.WriteLine("我是:"+tParameter.Country+"人");
            Console.WriteLine("姓名:" + tParameter.Name);

        }
    }

Chinese.cs、Japanese.cs和People.cs

    public class Chinese : People
    {

    }

    public class Japanese : People
    {

    }


    public class People
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }

    }

運行結果:

泛型類

GenericClass.cs

  class GenericClass<T>
    {
        private T a;
        private T b;

        public  GenericClass(T a,T b) {
            this.a = a;
            this.b = b;
        }

        public T Add() {
            dynamic v1 = a;
            dynamic v2 = b;
            return v1 + v2;
        }
    }

Program.cs

 static void Main(string[] args)
        {
            //泛型類
            GenericClass<int> gcA = new GenericClass<int>(1,2);
            int sumA = gcA.Add();
            Console.WriteLine("int類型值:"+sumA);
            GenericClass<string> gcB = new GenericClass<string>("字符", "串");
            string sumB = gcB.Add();
            Console.WriteLine("string類型值:" + sumB);
        }

運行結果

 

協變、逆變

協變的作用:解決用泛型包裝之後父子關係失效

現有兩個類People、Japanese,並且People和Japanese是父子關係

  People people = new Japanese(); //語法正確

  List<People> peoples = new List<People>();//語法正確

 List<People> peoples1 = new List<Japanese>();//語法錯誤

一個日本人是人,按理說一羣日本人屬於一羣人,那爲什麼一羣日本人就不是一羣人了呢?因爲在語義上是正確的的,但在語法上是錯誤的,List<People>是一個類,List<Japanese>也是一個類,它們兩個沒有父子關係。

可以通過下面代碼解決:
List<People> peoples2 = new List<Japanese>().Select(s=>(People)s).ToList();
也可以通過協變來解決
IEnumerable<People> birdList1 = new List<People>();
IEnumerable<People> birdList2 = new List<Japanese>();

逆變的作用:可以在右邊用上父類

 List<Japanese> japaneses = new List<Japanese>();//語法正確
 List<Japanese> japaneses1 = new List<People>();//語法錯誤

可以通過逆變來解決右邊無法使用父類的問題:

 Action<Japanese> act = new Action<People>((People i) => { });

 

泛型接口、泛型委託和泛型方法、泛型類幾乎都差不多,在這就不多寫了



demo地址:https://github.com/wangongshen/Generic.git

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章