c# 多键字典,"KeyNotFoundException" 用自己的可变 class 替换元组时
c# Multikey Dictionary, "KeyNotFoundException" when replacing Tuple by own mutable class
我声明了一个可变的 class 来替代元组作为字典的键。原因是序列化。序列化工作得很好。出现以下问题,我在使用此 class 时得到 "KeyNotFoundException",但前提是此 class 的新实例用于查找。为了更清楚地说明这一点,请参阅以下 class 定义:
public class STuple<T1, T2> {
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
public static implicit operator Tuple<T1, T2>(STuple<T1, T2> st)
{
return Tuple.Create(st.Item1, st.Item2);
}
public static implicit operator STuple<T1, T2>(Tuple<T1, T2> t)
{
return new STuple<T1, T2>()
{
Item1 = t.Item1,
Item2 = t.Item2,
};
}
public STuple()
{
}
public STuple(T1 t1, T2 t2) : this()
{
Item1 = t1;
Item2 = t2;
}
}
这里是示例程序:
Dictionary<Tuple<string, string>, double> TupleDic = new Dictionary<Tuple<string, string>, double>();
TupleDic.Add(new Tuple<string, string>("Name1", "Name2"), 5);
TupleDic.Add(new Tuple<string, string>("Name3", "Name4"), 10);
Console.WriteLine("dict-Entry 1: {0}", TupleDic[new Tuple<string, string>("Name1", "Name2")]);
Console.WriteLine("dict-Entry 2: {0}", TupleDic[new Tuple<string, string>("Name3", "Name4")]);
Dictionary<STuple<string, string>, double> STupleDic = new Dictionary<STuple<string, string>, double>();
STuple<string, string> STuple1 = new STuple<string, string>("Name1", "Name2");
STuple<string, string> STuple2 = new STuple<string, string>("Name3", "Name4");
STupleDic.Add(STuple1, 5);
STupleDic.Add(STuple2, 10);
//Still working
Console.WriteLine();
Console.WriteLine("Sdict-Entry 1: {0}", STupleDic[STuple1]);
Console.WriteLine("Sdict-Entry 2: {0}", STupleDic[STuple2]);
//Not working
STuple<string, string> STuple3 = new STuple<string, string>("Name1", "Name2");
STuple<string, string> STuple4 = new STuple<string, string>("Name3", "Name4");
Console.WriteLine();
Console.WriteLine("Sdict-Entry 1: {0}", STupleDic[STuple3]);
Console.WriteLine("Sdict-Entry 2: {0}", STupleDic[STuple4]);
Console.ReadKey();
使用普通元组的示例工作得很好,但是当我使用我自己的 class STuple 时,如果我使用与添加时使用的完全相同的键(相同的实例),它才有效。我是一个初学者,是否可能因为值类型和引用类型的混淆而出现问题?
在我看来真的很奇怪,使用 foreach 进行查找仍然有效:
Console.WriteLine();
foreach (KeyValuePair<STuple<string, string>, double> s in STupleDic)
{
Console.WriteLine("Sdict-Entry 1: {0}", s.Value);
}
警告:在 mutable 结构上实施 GetHashCode
会导致灾难。哈希码只有一个目的,那就是方便存储在 hash-tables 中。在 hash-tables 中用作键的项目不应发生变异(用于计算哈希的任何属性),因为更改哈希码会对 hash-table.[=20 造成不可恢复的损坏=]
为了让项目在 hash-table like 集合中工作,它们必须实现相等性和哈希码成员。因此,您可以(感谢 Resharper):
public class STuple<T1, T2>
{
public STuple()
{
}
public STuple(T1 t1, T2 t2)
: this()
{
Item1 = t1;
Item2 = t2;
}
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
protected bool Equals(STuple<T1, T2> other)
{
return EqualityComparer<T1>.Default.Equals(Item1, other.Item1) &&
EqualityComparer<T2>.Default.Equals(Item2, other.Item2);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((STuple<T1, T2>) obj);
}
public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T1>.Default.GetHashCode(Item1)*397) ^
EqualityComparer<T2>.Default.GetHashCode(Item2);
}
}
public static bool operator ==(STuple<T1, T2> left, STuple<T1, T2> right)
{
return Equals(left, right);
}
public static bool operator !=(STuple<T1, T2> left, STuple<T1, T2> right)
{
return !Equals(left, right);
}
public static implicit operator Tuple<T1, T2>(STuple<T1, T2> st)
{
return Tuple.Create(st.Item1, st.Item2);
}
public static implicit operator STuple<T1, T2>(Tuple<T1, T2> t)
{
return new STuple<T1, T2>
{
Item1 = t.Item1,
Item2 = t.Item2
};
}
}
我声明了一个可变的 class 来替代元组作为字典的键。原因是序列化。序列化工作得很好。出现以下问题,我在使用此 class 时得到 "KeyNotFoundException",但前提是此 class 的新实例用于查找。为了更清楚地说明这一点,请参阅以下 class 定义:
public class STuple<T1, T2> {
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
public static implicit operator Tuple<T1, T2>(STuple<T1, T2> st)
{
return Tuple.Create(st.Item1, st.Item2);
}
public static implicit operator STuple<T1, T2>(Tuple<T1, T2> t)
{
return new STuple<T1, T2>()
{
Item1 = t.Item1,
Item2 = t.Item2,
};
}
public STuple()
{
}
public STuple(T1 t1, T2 t2) : this()
{
Item1 = t1;
Item2 = t2;
}
}
这里是示例程序:
Dictionary<Tuple<string, string>, double> TupleDic = new Dictionary<Tuple<string, string>, double>();
TupleDic.Add(new Tuple<string, string>("Name1", "Name2"), 5);
TupleDic.Add(new Tuple<string, string>("Name3", "Name4"), 10);
Console.WriteLine("dict-Entry 1: {0}", TupleDic[new Tuple<string, string>("Name1", "Name2")]);
Console.WriteLine("dict-Entry 2: {0}", TupleDic[new Tuple<string, string>("Name3", "Name4")]);
Dictionary<STuple<string, string>, double> STupleDic = new Dictionary<STuple<string, string>, double>();
STuple<string, string> STuple1 = new STuple<string, string>("Name1", "Name2");
STuple<string, string> STuple2 = new STuple<string, string>("Name3", "Name4");
STupleDic.Add(STuple1, 5);
STupleDic.Add(STuple2, 10);
//Still working
Console.WriteLine();
Console.WriteLine("Sdict-Entry 1: {0}", STupleDic[STuple1]);
Console.WriteLine("Sdict-Entry 2: {0}", STupleDic[STuple2]);
//Not working
STuple<string, string> STuple3 = new STuple<string, string>("Name1", "Name2");
STuple<string, string> STuple4 = new STuple<string, string>("Name3", "Name4");
Console.WriteLine();
Console.WriteLine("Sdict-Entry 1: {0}", STupleDic[STuple3]);
Console.WriteLine("Sdict-Entry 2: {0}", STupleDic[STuple4]);
Console.ReadKey();
使用普通元组的示例工作得很好,但是当我使用我自己的 class STuple 时,如果我使用与添加时使用的完全相同的键(相同的实例),它才有效。我是一个初学者,是否可能因为值类型和引用类型的混淆而出现问题?
在我看来真的很奇怪,使用 foreach 进行查找仍然有效:
Console.WriteLine();
foreach (KeyValuePair<STuple<string, string>, double> s in STupleDic)
{
Console.WriteLine("Sdict-Entry 1: {0}", s.Value);
}
警告:在 mutable 结构上实施 GetHashCode
会导致灾难。哈希码只有一个目的,那就是方便存储在 hash-tables 中。在 hash-tables 中用作键的项目不应发生变异(用于计算哈希的任何属性),因为更改哈希码会对 hash-table.[=20 造成不可恢复的损坏=]
为了让项目在 hash-table like 集合中工作,它们必须实现相等性和哈希码成员。因此,您可以(感谢 Resharper):
public class STuple<T1, T2>
{
public STuple()
{
}
public STuple(T1 t1, T2 t2)
: this()
{
Item1 = t1;
Item2 = t2;
}
public T1 Item1 { get; set; }
public T2 Item2 { get; set; }
protected bool Equals(STuple<T1, T2> other)
{
return EqualityComparer<T1>.Default.Equals(Item1, other.Item1) &&
EqualityComparer<T2>.Default.Equals(Item2, other.Item2);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((STuple<T1, T2>) obj);
}
public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T1>.Default.GetHashCode(Item1)*397) ^
EqualityComparer<T2>.Default.GetHashCode(Item2);
}
}
public static bool operator ==(STuple<T1, T2> left, STuple<T1, T2> right)
{
return Equals(left, right);
}
public static bool operator !=(STuple<T1, T2> left, STuple<T1, T2> right)
{
return !Equals(left, right);
}
public static implicit operator Tuple<T1, T2>(STuple<T1, T2> st)
{
return Tuple.Create(st.Item1, st.Item2);
}
public static implicit operator STuple<T1, T2>(Tuple<T1, T2> t)
{
return new STuple<T1, T2>
{
Item1 = t.Item1,
Item2 = t.Item2
};
}
}