开发手册 欢迎您!
软件开发者资料库

C#(.NET Core) 泛型<T>中协变(covariant)和逆变(contravariant)的使用

泛型很有用,可减少代码,更好得复用代码。本文主要介绍一下C#(.NET Core) 泛型中使用协变(covariant)和逆变(contravariant)的方法,及使用的示例代码。

1、泛型中协变(covariant)

简单来说,协变用法是只能放在接口或者委托的泛型参数前面,是用来修饰返回值。具体使用如下代码:

    ///     /// out 协变 只能是返回结果    ///     ///     public interface ICustomerListOut    {        T Get();        //void Show(T t);    }    public class CustomerListOut : ICustomerListOut    {        public T Get()        {            return default(T);        }        //public void Show(T t)        //{        //}    }

2、泛型中逆变(contravariant)

也是只能放在接口或者委托的泛型参数前面,但是用来修饰传入参数。具体使用如下代码:

 public interface IMyList    {        void Show(inT t);        outT Get();        outT Do(inT t);        ////out 只能是返回值   in只能是参数        //void Show1(outT t);        //inT Get1();    }

3、协变和逆变的使用

协变和逆变都是用在接口或者委托的泛型参数前面,一个用来修饰返回值,一个是修饰传入参数,使用示例代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Cjavapy.Extend{    ///     /// .net4.0    /// 只能放在接口或者委托的泛型参数前面    /// out 协变covariant    修饰返回值     /// in  逆变contravariant  修饰传入参数    ///     public class CCTest    {        public static void Show()        {            {                Bird bird1 = new Bird();                Bird bird2 = new Sparrow();                Sparrow sparrow1 = new Sparrow();                //Sparrow sparrow2 = new Bird();            }            {                List birdList1 = new List();                //List birdList2 = new List();                //应该可以呀   一堆麻雀当然是一堆鸟                //没有父子关系                List birdList3 = new List().Select(c => (Bird)c).ToList();            }            {//协变                IEnumerable birdList1 = new List();                IEnumerable birdList2 = new List();                Func func = new Func(() => null);                ICustomerListOut customerList1 = new CustomerListOut();                ICustomerListOut customerList2 = new CustomerListOut();            }            {//逆变                ICustomerListIn customerList2 = new CustomerListIn();                ICustomerListIn customerList1 = new CustomerListIn();                ICustomerListIn birdList1 = new CustomerListIn();                birdList1.Show(new Sparrow());                birdList1.Show(new Bird());                Action act = new Action((Bird i) => { });            }            {                IMyList myList1 = new MyList();                IMyList myList2 = new MyList();//协变                IMyList myList3 = new MyList();//逆变                IMyList myList4 = new MyList();//逆变+协变            }        }    }    public class Bird    {        public int Id { get; set; }    }    public class Sparrow : Bird    {        public string Name { get; set; }    }    public interface ICustomerListIn    {        //T Get();        void Show(T t);    }    public class CustomerListIn : ICustomerListIn    {        //public T Get()        //{        //    return default(T);        //}        public void Show(T t)        {        }    }    ///     /// out 协变 只能是返回结果    ///     ///     public interface ICustomerListOut    {        T Get();        //void Show(T t);    }    public class CustomerListOut : ICustomerListOut    {        public T Get()        {            return default(T);        }        //public void Show(T t)        //{        //}    }    public interface IMyList    {        void Show(inT t);        outT Get();        outT Do(inT t);        ////out 只能是返回值   in只能是参数        //void Show1(outT t);        //inT Get1();    }    public class MyList : IMyList    {        public void Show(T1 t)        {            Console.WriteLine(t.GetType().Name);        }        public T2 Get()        {            Console.WriteLine(typeof(T2).Name);            return default(T2);        }        public T2 Do(T1 t)        {            Console.WriteLine(t.GetType().Name);            Console.WriteLine(typeof(T2).Name);            return default(T2);        }    }}

相关文档:C#(.NET Core)使用泛型实现类型数据缓存方法及示例代码