Skip to main content
 首页 » 编程设计

c#之字典键存在时找不到

2024年02月06日14think

为什么我的字典在我刚插入时说一个键不存在?与我的Equals 方法或双重比较有关吗?

代码如下:

// Test: I know the dictionary contains nCoord but its saying the key doesn't exist 
Dictionary<UTMCoordinate, int> planes = new Dictionary<UTMCoordinate, int>(); 
UTMCoordinate nCoord    = new UTMCoordinate(337394.136407966, 6263820.40182064, 0, 56, UTMCoordinate.Hemisphere.H_SOUTHERN); 
planes[nCoord]          = 1; 
 
bool exists = planes.ContainsKey(nCoord);  // always returns false 

我对 UTMCoordinate 的实现如下:

public class UTMCoordinate 
{ 
    public enum                 Hemisphere {H_NOTHERN, H_SOUTHERN}; 
    public const double         DIF_TOLERANCE    = 0.0005; 
    public double               x               { get; set; } 
    public double               y               { get; set; } 
    public double               elev            { get; set; } 
    public uint                 UTMZone         { get; set; } 
    public Hemisphere           hemisphere      { get; set; } 
 
    public UTMCoordinate(double x, double y, double elev=double.MinValue, uint utmZone=uint.MinValue, Hemisphere hemisphere=Hemisphere.H_SOUTHERN) { 
        this.x          = x; 
        this.y          = y; 
        this.elev       = elev; 
        this.UTMZone    = utmZone; 
        this.hemisphere = hemisphere; 
    } 
 
    public override int GetHashCode() { 
        unchecked // Overflow is fine, just wrap 
        { 
            int hash = 17; 
            // Suitable nullity checks etc, of course :) 
            hash = hash * 23 + x.GetHashCode(); 
            hash = hash * 23 + y.GetHashCode(); 
            hash = hash * 23 + elev.GetHashCode(); 
            hash = hash * 23 + UTMZone.GetHashCode(); 
            hash = hash * 23 + hemisphere.GetHashCode(); 
            return hash; 
        } 
    } 
 
    public override bool Equals(object obj) 
    { 
        UTMCoordinate other = obj as UTMCoordinate; 
        if (other == null) 
            return false; 
 
        return double.Equals(x, other.x) && double.Equals(y, other.y) && double.Equals(elev, other.elev) && uint.Equals(UTMZone, other.UTMZone) && double.Equals(hemisphere, other.hemisphere); 
    } 
} 

编辑 根据 Daniel A. White 的建议,我使用了不同的双重比较方法。不幸的是它仍然没有识别 key :

public override bool Equals(object obj) 
{ 
    //return base.Equals (obj); 
    UTMCoordinate other = obj as UTMCoordinate; 
    if (other == null) 
        return false; 
 
    //return double.Equals(x, other.x) && double.Equals(y, other.y) && double.Equals(elev, other.elev) && uint.Equals(UTMZone, other.UTMZone) && double.Equals(hemisphere, other.hemisphere); 
    return Math.Abs (x-other.x) <= DIF_TOLERANCE && Math.Abs (y-other.y) <= DIF_TOLERANCE && Math.Abs (elev-other.elev) <= DIF_TOLERANCE && uint.Equals(UTMZone, other.UTMZone) && hemisphere == other.hemisphere; 
} 

请您参考如下方法:

如果我从你的问题中提取你的代码:

Dictionary<UTMCoordinate, int> planes = new Dictionary<UTMCoordinate, int>(); 
UTMCoordinate nCoord    = new UTMCoordinate(337394.136407966, 6263820.40182064, 0, 56, UTMCoordinate.Hemisphere.H_SOUTHERN); 
planes[nCoord]          = 1; 
 
bool exists = planes.ContainsKey(nCoord); 

我为 exists 得到的值是 true。

但是,如果我这样做:

nCoord.x = 1.0; 
exists = planes.ContainsKey(nCoord); 

exists 的值突然变为 false,即使该对象仍在字典中。这是因为 GetHashCode 的值已更改。它是 -1473667404,但在属性 x 赋值后它变成了 201352392。

字典使用 GetHashCode 的值来确定将键放入哪个桶,因此当哈希码更改时,字典可能会尝试在错误的桶中找到键,然后报告它没有t 包含 key 。

我怀疑您的代码中正在发生这种情况。

因此您需要更改您的对象,使其不可变。

public double               x               { get; private set; } 
public double               y               { get; private set; } 
public double               elev            { get; private set; } 
public uint                 UTMZone         { get; private set; } 
public Hemisphere           hemisphere      { get; private set; } 

然后不要更改构造函数之外的任何值。