這裏是一些C#代碼無法編譯,給下面的消息:無法從「出T」轉換成「輸出分量」
不能從轉換「出來T」爲「輸出分量」
public void Get<T>(out T c) where T : Component
{
m_components.TryGetValue(typeof(T), out c);
}
這裏是代碼,不會編譯:
public void Get<T>(out T c) where T : Component
{
Component temp;
m_components.TryGetValue(typeof(T), out temp);
c = (T)temp;
}
我不知道爲什麼第一個代碼無效因爲「其中T:組件」明確指出T是類型組件。
感謝
如果將T定義爲Component類型,爲什麼需要將「Get」作爲泛型方法? 可能是你沒有提到這裏的確切場景。 –
2009-12-03 06:43:37
* twiddles拇指等待Skeet先生在這個有趣的問題上啓發我們* – tster 2009-12-03 06:44:04
這是一個重複。 http://stackoverflow.com/questions/1207144/c-why-doesnt-ref-and-out-support-polymorphism/1207302#1207302 – 2009-12-03 15:52:02