我想我必须遗漏一些东西,为什么我不能编译这个:
class Foo<T> where T : Bar
{
T Bar;
}
abstract class Bar
{ }
class MyBar : Bar
{ }
static void Main(string[] args)
{
var fooMyBar = new Foo<MyBar>();
AddMoreFoos(fooMyBar);
}
static void AddMoreFoos<T>(Foo<T> FooToAdd) where T : Bar
{
var listOfFoos = new List<Foo<Bar>>();
listOfFoos.Add(FooToAdd); //Doesn't compile
listOfFoos.Add((Foo<Bar>)FooToAdd); //doesn't compile
}
Run Code Online (Sandbox Code Playgroud)
你在这里使用列表让事情变得比他们需要的更混乱......最容易看到效果:
// This won't compile
Foo<Bar> fooBar = new Foo<MyBar>();
Run Code Online (Sandbox Code Playgroud)
鉴于这不能编译,所以你不能添加一个Foo<MyBar>就不足为奇了List<Foo<Bar>>
那么为什么不是Foo<MyBar>一个Foo<Bar>?因为泛型类不是协变的.
通用方差仅在C#4中引入 - 它仅适用于接口和委托.所以你可以(在C#4中)做:
IEnumerable<MyBar> x = new List<MyBar>();
IEnumerable<Bar> y = x;
Run Code Online (Sandbox Code Playgroud)
但你做不到:
IList<MyBar> x = new List<MyBar>();
IList<Bar> y = x;
Run Code Online (Sandbox Code Playgroud)
我有一个关于方差的讨论,您可以从NDC 2010视频网站下载- 只需搜索"差异".
| 归档时间: |
|
| 查看次数: |
800 次 |
| 最近记录: |