任何人都可以解釋這一點嗎?在通用方法中使用Equals
void Main()
{
int a = 1;
short b = 1;
Console.WriteLine(b.Equals(a)); // false
Console.WriteLine(a.Equals(b)); // true
Test<int, short>(a, b); // prints: false, false
Test(a, b); // prints: false, false
}
void Test<T1, T2>(T1 x, T2 y)
{
Console.WriteLine(y.Equals(x));
Console.WriteLine(x.Equals(y));
}
(不會讓我發佈無需添加一些文字,所以這裏的一些更多...)
也看看這個:[什麼時候C#中的泛型類型解析?](http://stackoverflow.com/questions/17734247/when-is-the-generic-type-resolved-in-c) – Costas
和這[這是爲什麼通用在編譯時解決?(http://stackoverflow.com/questions/6127855/why-is-this-generic-not-resolved-at-compile-time) – Costas
望着IL,通用方法中的代碼始終是裝箱的,而「main」方法中的代碼不是。 – Costas