一:什么是协变与逆变
协变指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,逆变指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型
只有泛型接口和泛型委托参数支持协变和逆变
二:引言
1
2
3
4
5
6
7
8
9
10
11
12
|
using System; using System.Collections.Generic; class MainClass { static void Main() { object o = "str" ; List< object > oList = new List< string >(); IEnumerable< object > strs = new List< string >(); } } |
上面这段代码中,第一句没问题,属于类型安全转换,第二句会报错,因为这两个list并没有继承关系,而第三句是正确的,其实在背后,就是协变和逆变在起作用
三:协变
协变在泛型方法的参数里以out表示,使用out可以在声明父类泛型参数的时候使用子类泛型参数构造,out参数可以单纯的理解为输出,作为返回值例如IEnumerable<T>接口
1
2
3
4
5
6
7
8
9
10
|
using System; using System.Collections.Generic; class MainClass { static void Main() { IEnumerable< object > list = new List< string >(); } } |
分析一下上面的代码为什么是合法的呢?首先虽然是用IEnumerable<object>声明的,但是是用List<string>构造的,列表中的元素是string类型。其次IEnumerable的作用只有遍历元素,不允许添加操作,所以是合法的,本质上就是里氏替换原则
四:逆变
逆变在泛型方法的参数里以in表示,使用in可以在声明子类泛型参数的时候使用父类泛型参数构造,int参数只能作为传入值不能作为返回值例如Action<T>委托
1
2
3
4
5
6
7
8
9
10
|
using System; class MainClass { static void Main() { Action< string > action = new Action< object >((o)=> { }); action( "" ); } } |
分析一下上面的代码为什么是合法的呢?看似是object转换成了string,但实际上使用委托的时候传入的是一个string类型的参数,然后将string转换成了object,本质上还是派生类到基类的转换,所以是类型安全的,本质上就是里氏替换原则
五:为什么协变和逆变是针对泛型接口或泛型委托参数的?
而不能针对泛型类?
由上可知,协变和逆变都是定义方法成员的(接口不能定义字段只能定义成员),而方法成员在创建对象时是不涉及到对象内存分配的,所以是类型安全的,而泛型类是模板类,类中可以包含字段, 所以是不安全的
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using System; using System.Collections.Generic; class MainClass { static void Main() { object o1 = "" ; //类型安全 string s1 = ( string )o1; //非类型安全 IEnumerable< object > o2 = new List< string >(); //协变 Action< string > s2 = new Action< object >((o) => { }); //逆变 } } |
六:自定义协变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System; using System.Collections.Generic; class MainClass { static void Main() { ICustomCovariant< object > o = new CustomCovariant< string >(); } } public interface ICustomCovariant< out T> { T Get(); } public class CustomCovariant< T> : ICustomCovariant<T> { public T Get() { return default (T); } } |
七:自定义逆变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System; using System.Collections.Generic; class MainClass { static void Main() { IContravariant< string > o = new CustomContravariant< object >(); } } public interface IContravariant< in T> { void Get(T t); } public class CustomContravariant<T> : IContravariant<T> { public void Get(T t) { } } |
八:总结
——在泛型中,如果确定泛型参数是只读或者只写的,那么就可以使用协变或者逆变。如果泛型参数无法确定只读或只写,这种类型参数既不能协变也不能逆变,只能精确类型匹配 ——在泛型或委托中,如果不使用协变或逆变,那么泛型类型是一个固定类型,而使用协变或逆变的话,则泛型类型可以实现多态化
到此这篇关于C#中的协变与逆变的文章就介绍到这了,更多相关C#协变与逆变内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/LLLLL__/article/details/120966813