I cannot find a dictionary entry by key. I have an interface like the following:
public interface IFieldLookup
{
string FileName { get; set; }
string FieldName { get; set; }
}
Then I have a dictionary like so:
Dictionary<IFieldLookup, IField> fd
When I try to retrieve an element out of the dictionary by the key, I get a KeyNotFoundException. I am assuming that I have to implement some type of comparison – if my assumption is correct, what is the recommended way of implementing a comparison in this case?
Since this is an interface rather than a class, you will have to define your equality operator for every class that implements the interface. And those operators will need to operate consistantly. (This would be much better if it were a class rather than an interface.)
You must override the
Equals(object)andGetHashCode()methods on each class.Probably something like this:
or this:
Depending on how you want it to behave.