I'm trying to figure out how to use custom class as key in dictionary. My class
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;
}
}
And when I try to use it:
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.
What am I doing wrong?