我有以下代码,可以在.NET Framework 4.0及以上版本中编译:
public abstract class MyBase { }
public class MyDerived : MyBase { }
public abstract class MyBaseCollection<T> : IList<T> where T : MyBase
{
protected readonly IList<T> deriveds = new List<T>();
public void Test()
{
// This line works in .NET versions 4.0 and above, but not in versions below.
IEnumerable<MyBase> bases = deriveds;
}
#region IList members with NotImplementedException
// ...
#endregion
}
public class MyDerivedCollection : MyBaseCollection<MyDerived> { }
但是在 .NET Framework 4.0 以下的版本中,我会在以下代码行上收到编译错误:
IEnumerable<MyBase> bases = deriveds;
无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?)
.NET 4.0 在此方面有什么变化(或引入了什么)?
是否有任何相关文档?