泛型接口协变逆变的几个问题
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
假设: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
相关文章推荐
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
- C#里泛型接口支持协变、逆变和不支持协变、逆变的对比?
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- 泛型在接口中的协变、逆变练习
- c#4.0泛型接口和泛型委托的协变和逆变
- [译]委托和接口泛型参数类型的协变和逆变
- 第五节:泛型(泛型类、接口、方法、委托、泛型约束、泛型缓存、逆变和协变)
- <转>C# 4.0 为泛型编程引入了 协变 和 逆变 支持,这是个不错的福利,能省掉以往的一些麻烦。不过当前(Beta2)仅支持泛型接口和泛型委托。
- <转>C# 4.0 为泛型编程引入了 协变 和 逆变 支持,这是个不错的福利,能省掉以往的一些麻烦。不过当前(Beta2)仅支持泛型接口和泛型委托。
- 泛型接口的协变和逆变
- 理解 C# 泛型接口中的协变与逆变(抗变)
- 泛型的几个较困惑的问题
- 泛型中的协变和逆变
- C# 泛型的协变和逆变 (转载)
- 关于 Spring 中 Autowired 注入接口的几个问题
- C# 入门(13) 泛型(generic)、协变(convariance)、逆变(contravariance)
- .NET 4.0中的泛型的协变和逆变
- C#中的foreach语句与枚举器接口(IEnumerator)及其泛型 相关问题
- C# 泛型接口的抗变和协变
- 警惕泛型是不能协变和逆变的