I want to use a custom object as a Dictionary key, mainly, I have something like this: (I can't use .net 4.0 so I don't have tuples)
class Tuple<A, B> : IEquatable<Tuple<A,B>>
{
public A AValue { get; set; }
public B BValue { get; set; }
public Tuple(A a, B b){ AValue = a; BValue = b; }
public bool Equals(Tuple<A, B> tuple)
{
return tuple.AValue.Equals(AValue) && tuple.BValue.Equals(BValue);
}
public bool Equals(object o)
{
return this.Equals(o as Tuple<A,B>);
}
}
I then do something like this.
var boolmap = new Dictionary<Tuple<bool, bool>, string>();
boolmap.Add(new Tuple<bool,bool>(true, true), "A");
boolmap.Add(new Tuple<bool,bool>(true, false), "B");
boolmap.Add(new Tuple<bool,bool>(false, true), "C");
boolmap.Add(new Tuple<bool,bool>(false, false), "D");
var str = boolmap[new Tuple<bool,bool>(true, false)];
I get a KeyNotFound exception at the last line. Why is this ? Isn't it enough I implement IEquatable ?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…