ASP.NET 使用List.Remove 不生效

   首先看看 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

相关文章
相关标签/搜索