gpt4 book ai didi

c# - 字典中包含 long 的类的唯一键

转载 作者:行者123 更新时间:2023-11-30 13:40:18 24 4
gpt4 key购买 nike

如果我想使用这个类的对象作为字典中的键,我需要做什么? (10.0, 20.0) 不应作为键存在两次。

public class IntPoint
{
public Int64 X { get; set; }
public Int64 Y { get; set; }

public IntPoint(Int64 X, Int64 Y)
{
this.X = X; this.Y = Y;
}

public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
}

编辑

public static Dictionary<IntPoint, List<int>> GetPolygonDuplicateIndixes(Polygon p)
{
Dictionary<IntPoint, List<int>> d = new Dictionary<IntPoint, List<int>>();
int i = 0;
foreach(IntPoint point in p)
{
if(!d.ContainsKey(point))
{
d[point] = new List<int>();
}
d[point].Add(i);
i++;
}
...

我在 d 中得到重复项。为什么? 22002、1501在p中出现了两次。

最佳答案

如果您查看 Dictionary documentation ,你会看到如果键实现 IEquatable ,将改用该平等实现。

关于c# - 字典中包含 long 的类的唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com