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;
?