首先看看 List<T> 是如何實現 IList.Remove 的:html
This method determines equality using the default equality comparer EqualityComparer.Default for T, the type of values in the list.
原來,List<T> 在 IList.Remove 中使用 EqualityComparer.Default 來判斷兩個對象是否相等。那麼 EqualityComparer.Default 又是如何得知兩個對象是否相等呢?c#
The Default property checks whether type T implements the System.IEquatable generic interface and if so returns an EqualityComparer that uses that implementation. Otherwise it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T.
咱們能夠發現 List<T> 中的 IList.Remove 判斷兩個 Product 對象是否相等的方法是從 Object 根類繼承下來的 Equals 和 GetHashCode 方法,即比較兩個對象的引用是否指向同一個對象。ide
解決方案:oop
例:
this
public override bool Equals(object obj) { if(this.GetType() == typeof(Product)){ return true; } return false; }
原文連接:http://www.cnblogs.com/allenlooplee/archive/2007/01/06/613608.htmlspa