I'm trying to implement an IEqualityComparer on a sub class which will be stored as a key to a dictionary.
the following is what i have
public class SuperClass : IEqualityComparer<SuperClass> {
public virtual bool Equals(SuperClass dictKeyComparerA, SuperClass dictKeyComparerB) {
throw new NotImplementedException();
}
public virtual int GetHashCode(SuperClass dictKeyComparer) {
throw new NotImplementedException();
}
}
public class SubClass : SuperClass {
private readonly string application;
public SubClass(string application) {
this.application = application;
}
public override bool Equals(SuperClass dictKeyComparerA, SuperClass dictKeyComparerB) {
throw new NotImplementedException();
}
public override int GetHashCode(SuperClass dictKeyComparer) {
throw new NotImplementedException();
}
}
and i have the following to try to search the dictionary:
Dictionary<SubClass, List<string>> TestDict3 = new Dictionary<SubClass, List<string>>(new SubClass("random"));
var testKeyb = new SubClass("12345");
var testListb = new List<string>{"aaaaa"};
TestDict3.Add(testKeyb, testListb);
var testKey2b = new SubClass("12345");
var testKey3b = new SubClass("56973");
if(TestDict3.ContainsKey(testKey2b)) {
Console.WriteLine("found testKey2b");
}
else {
Console.WriteLine("did not found testKey2b");
}
if(TestDict3.ContainsKey(testKey3b)) {
Console.WriteLine("found testKey3b");
}
else {
Console.WriteLine("did not found testKey3b");
}
When the code is ran, it returns a not implemented exception because it is calling the super class GetHashCode function instead of the override function.
Any thoughts on the issue?