您的位置:首页 > 其它

泛型接口协变逆变的几个问题

2012-12-04 09:30 393 查看
1、什么是协变、逆变?

假设:TSub是TParent的子类。
协变:如果一个泛型接口IFoo<T>,IFoo<TSub>可以转换为IFoo<TParent>的话,我们称这个过程为协变,IFoo支持对参数T的协变。
逆变:如果一个泛型接口IFoo<T>,IFoo<TParent>可以转换为IFoo<TSub>的话,我们称这个过程为逆变,IFoo支持对参数T的逆变。

2、为什么要有协变、逆变?

通常只有具备继承关系的对象才可以发生隐式类型转换,如Base b=new sub()。
协变和逆变可以使得更多的类型之间能够实现隐式类型转换、类型安全性有了保障。

3、为什么泛型接口要引入协变、逆变?

基于以上原因的同时、许多接口仅仅将类型参数用于参数或返回值。所以支持协变和逆变后泛型的使用上有了更大的灵活性

4、为什么支持协变的参数只能用于方法的返回值?支持逆变的参数只能用于方法参数?


“TParent不能安全转换成TSub”,是这两个问题的共同原因。
我们定义一个接口IFoo。

interface IFoo<T>
{
void Method1(T param);
T Method2();
}

我们看一下协变的过程:IFoo<TSub>转换成IFoo<TParent>。

Method1:将TSub替换成TParent,Method1显然存在 TParent到TSub的转换。

Method2:返回值类型从TSub换成了TParent,是类型安全的。

所以支持协变的参数只能用在方法的返回值中。

再看一下逆变的过程:IFoo<TParent>转换成IFoo<TSub>。

Method1:将TParent替换成TSub,Method1存在 TSub到TParent的转换,是类型安全的。

Method2:返回值类型从TParent换成了TSub,是不安全的。

所以支持逆变的参数只能用在方法的参数中。

5、泛型接口支持协变、逆变和不支持协变、逆变的对比?


这其实是对3个问题的补充。

定义一个接口IFoo,既不支持协变,也不支持逆变。

interface IFoo<T>
{
void Method1(T param);
T Method2();
}

实现接口IFoo

public class FooClass<T> : IFoo<T>
{
public void Method1(T param)
{
Console.WriteLine(default(T));
}
public T Method2()
{
return default(T);
}
}

定义一个接口IBar支持对参数T的协变

interface IBar<out T>
{
T Method();
}

实现接口IBar

public class BarClass<T> : IBar<T>
{
public T Method()
{
return default(T);
}
}

定义一个接口IBaz支持对参数T的逆变

interface IBaz<in T>
{
void Method(T param);
}

实现接口IBaz

public class BazClass<T> : IBaz<T>
{
public void Method(T param)
{
Console.WriteLine(param.ToString());
}
}

定义两个有继承关系的类型,IParent和SubClass。

interface IParent
{
void DoSomething();
}
public class SubClass : IParent
{
public void DoSomething()
{
Console.WriteLine("SubMethod");
}
}

按照协变的逻辑,分别来使用IFoo和IBar。

//IFoo 不支持对参数T的协变
IFoo<SubClass> foo_sub = new FooClass<SubClass>();
IFoo<IParent> foo_parent = foo_sub;//编译错误

//IBar 支持对参数T的协变
IBar<SubClass> bar_sub = new BarClass<SubClass>();
IBar<IParent> bar_parent = bar_sub;

foo_parent = foo_sub 会提示编译时错误“无法将类型“IFoo<SubClass>”隐式转换为“IFoo<IParent>”。存在一个显式转换(是否缺少强制转换?)”

按照逆变的逻辑,分别来使用IFoo和IBaz。

//IFoo 对参数T逆变不相容
IFoo<IParent> foo_parent = null;
IFoo<SubClass> foo_sub = foo_parent;//编译错误

//IBaz 对参数T逆变相容
IBaz<IParent> baz_parent = null;
IBaz<SubClass> baz_sub = baz_parent;

foo_sub = foo_parent 会提示编译时错误“无法将类型“IFoo<IParent>”隐式转换为“IFoo<ISub>”。存在一个显式转换(是否缺少强制转换?)”

6、.NET4.0对IEnumerable接口的修改?

2.0中的定义:

public interface IEnumerable<T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}

4.0中的定义:

public interface IEnumerable<out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}

可以看到4.0中增加了对协变的支持。

可以在两个版本试下, 下面的语句在2.0下会报错。

List<SubClass> subarr = new List<SubClass>();
IEnumerable<IParent> parentarr = subarr;

参考:
http://technet.microsoft.com/zh-CN/library/dd799517 http://www.cnblogs.com/Ninputer/archive/2008/11/22/generic_covariant.html http://www.cnblogs.com/idior/archive/2010/06/20/1761383.html http://www.cnblogs.com/artech/archive/2011/01/13/variance.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐