我正在获取字典中的第一个键并将其与查找值进行比较。它们是相同的,但是当我尝试从字典中访问密钥时,我得到KeyNotFoundException
:
public void Draw(Graphics graphics, Shape shape)
{
foreach (var line in shape.Lines)
{
var tuple = shape.LinesToNurmalDictionary.Keys.First();
bool equals = tuple == line;
bool referenceEquals = ReferenceEquals(tuple, line);
var invisible = shape.LinesToNurmalDictionary[line].All(x => x.Z < 0);
我该如何解决?
添加了: 字典是
Dictionary<Tuple<ShapePoint, ShapePoint>, List<ShapePoint>> LinesToNurmalDictionary;
所以我通过使用我自己的课而不是Tuple解决了我的问题,但实际问题仍然没有回应:
public class Line
{
public ShapePoint A { get; set; }
public ShapePoint B { get; set; }
public List<ShapePoint> Normals { get; set; }
public Line(ShapePoint a, ShapePoint b)
{
A = a;
B = b;
Normals = new List<ShapePoint>();
}
private sealed class DefaultEqualityComparer : IEqualityComparer<Line>
{
public bool Equals(Line x, Line y)
{
if (ReferenceEquals(x, y))
return true;
if (ReferenceEquals(x, null))
return false;
if (ReferenceEquals(y, null))
return false;
if (x.GetType() != y.GetType())
return false;
return Equals(x.A, y.A) && Equals(x.B, y.B);
}
public int GetHashCode(Line obj)
{
unchecked
{
return ((obj.A != null ? obj.A.GetHashCode() : 0)*397) ^ (obj.B != null ? obj.B.GetHashCode() : 0);
}
}
}
private static readonly IEqualityComparer<Line> DefaultComparerInstance = new DefaultEqualityComparer();
public static IEqualityComparer<Line> DefaultComparer
{
get
{
return DefaultComparerInstance;
}
}
}
答案 0 :(得分:2)
正如tia在评论中已经说过的那样,这是因为条目首先被添加到Dictionary<,>
,其中密钥的哈希码(这里是Tuple<ShapePoint, ShapePoint>
)具有一些“已保存”的值由Dictionary<,>
保存。之后,密钥对象以改变其哈希码的方式“变异”(修改)。这导致Dictionary<,>
处于腐败状态。
因此,当之后搜索密钥时,找不到密钥。它的“新”哈希码与Dictionary<,>
认为该键具有的哈希值不同。
请勿以更改其哈希码的方式修改Dictionary<,>
(或HashSet<>
的成员等)中可能是键的对象。
这是一个示例说明。类型:
class Changeable
{
public int Prop { get; set; }
public override int GetHashCode()
{
return Prop;
}
public override bool Equals(object obj)
{
return Equals(obj as Changeable);
}
public bool Equals(Changeable other)
{
if (other == null)
return false;
return GetType() == other.GetType() && Prop == other.Prop;
}
}
提出同样问题的代码:
var onlyInstance = new Changeable();
var dict = new Dictionary<Changeable, string>();
onlyInstance.Prop = 1;
dict.Add(onlyInstance, "what-ever");
onlyInstance.Prop = 2;
string restoredValue = dict[onlyInstance]; // throws!