我试图弄清楚如何在字典中使用自定义类作为键。我的班级
public class Enabler
{
public string Name { get; set; }
public bool Enabled { get; set; }
public override int GetHashCode()
{
return Name.GetHashCode() ^ Enabled.GetHashCode();
}
public override bool Equals(object obj)
{
return Equals(obj as Enabler);
}
public bool Equals(Enabler obj)
{
return obj != null && obj.Name == this.Name;
}
}
当我尝试使用它时:
Dictionary<Enabler, List<AppSettingsElement>> appSettings = new Dictionary<Enabler, List<AppSettingsElement>>();
appSettings.Add(new Enabler {Name = "none", Enabled = true }, new List<AppSettingsElement>());
Enabler myKey = new Enabler { Name = "none" };
appSettings[myKey].Add(new AppSettingsElement { Comment = text, Name = name, Value = "value" });
//last line throws "myKey not found in dictionary" error.
我做错了什么?
答案 0 :(得分:4)
您的Equals
和GetHashCode
方法需要兼容。目前,您的GetHashCode
方法会考虑Enabled
,但Equals
则不会,这会导致事情无法解决。从Enabled
删除GetHashCode
支票:
public override int GetHashCode()
{
return Name.GetHashCode();
}
或将其添加到Equals
:
public bool Equals(Enabler obj)
{
return obj != null && obj.Name == this.Name
&& obj.Enabled == this.Enabled;
}
注意到使用第二个选项时,myKey
需要Enabled = true
才能匹配。
注意:可变键是一个可怕的坏主意。你应该考虑:
public Enabler(string name, bool enabled)
{
Name = name;
Enabled = enabled;
}
public string Name { get; }
public bool Enabled { get; }
使其不可变。
注意:如果你使它成为不可变的,那么它也可能是struct
,所以:
public struct Enabler
{ ... }
并修复null
支票:
public override bool Equals(object obj)
=> Equals(obj is Enabler e && Equals(e));
public bool Equals(Enabler obj)
=> obj.Name == this.Name && obj.Enabled == this.Enabled;
无论哪种方式,您还应该Enabler
实施IEquatable<Enabler>
,即
public class Enabler : IEquatable<Enabler>
或
public struct Enabler : IEquatable<Enabler>