static IEnumerable<U> DoSomething<T, U>(IEnumerable<T> a)
where T : U
{
// Works, compiler can compile-time statically cast
// T as U.
T testA = default(T);
U testB = testA;
// And the following works, though:
IEnumerable<string> test2A = null;
IEnumerable<object> test2B = test2A;
// Doesn’t work. Compiler cannot compile-time statically
// cast IEnumerable<T> to IEnumerable<U> even though it is
// out and T is U.
return a;
}
我有代码在哪里能够执行这种类型的隐式转换将节省我写了很多样板接口实现代码。 这似乎是协方差本应帮助的事情。 但我总是得到这个错误在return a;
线之上:为什么协变隐式转换忽略通用约束?
错误CS0266:无法键入“System.Collections.Generic.IEnumerable <牛逼>”隐式转换为“System.Collections.Generic.IEnumerable <ü> ”。一个显式转换存在(是否缺少强制转换?)
这是为什么这样,是有办法解决这个没有做这样的事情return from o in a select o;
?