为什么以下'exist'布尔变量的值为false ???字典密钥不包含已包含在密钥中的密钥
foreach (Cell existCell in this.decoratorByCell.Keys)
{
//this call yield the same hashcode for both cells. still exist==false
bool exist =
this.decoratorByCell.ContainsKey(existCell);
}
我重写GetHashCode()方法&的equals()方法如下:
public override int GetHashCode()
{
string nodePath = GetNodePath();
return nodePath.GetHashCode() + m_ownerColumn.GetHashCode();
}
public bool Equals(Cell other)
{
bool nodesEqual = (other.OwnerNode == null && this.OwnerNode == null) || (other.GetNodePath() == this.GetNodePath());
bool columnsEqual = (other.OwnerColumn == null && this.OwnerColumn == null) || (other.OwnerColumn == this.OwnerColumn);
bool treesEqual = (this.m_ownerTree == other.m_ownerTree);
return (nodesEqual && columnsEqual && treesEqual);
}
.NET框架文档实际上有一个更好的描述它必须做什么,并澄清你的#1应该做的情况下:http://msdn.microsoft.com/en-us/library/system.object.gethashcode的.aspx – jasonh 2010-04-25 15:59:45