I’ve read the MSDN documentation on how Dictionary.ContainsKey() works, but I was wondering how it actually makes the equality comparison? Basically, I have a dictionary keyed to a reference type* and I want the ContainsKey() method to check a certain property of that reference type as its basis for determining if the key exists or not. For example, if I had a Dictionary(MyObject, int) and MyObject has a public property (of int) called “TypeID”, could I get ContainsKey(MyObject myObject) to check to see if one of the keys has a TypeID that is equal to myObject? Could I just overload the == operator?
- The reference type is an object called “Duration” which holds a value (
double Length); “Duration” is a base type used in my music program to denote how long a particular sound lasts. I derive classes from it which incorporate more sophisticated timing concepts, like Western musical time signatures, but want all of them to be comparable in terms of their length.
EDIT: As suggested, I implemented IEquitable on my object like so:
public class Duration : IEquatable<Duration>
{
protected double _length;
/// <summary>
/// Gets or Sets the duration in Miliseconds.
/// </summary>
public virtual double Length
{
get
{
return _length;
}
set
{
_length = value;
}
}
// removed all the other code that as it was irrelevant
public override bool Equals(object obj)
{
Duration otherDuration = (Duration)obj;
if (otherDuration._length == _length)
{
return true;
}
else
{
return false
}
}
}
Is this all I need to do?
EDIT: here is code for your updated example. Note: I find it a little odd that you expose the field as protected, and also have a virtual property that exposes the member. Under this scheme something could override
Lengthresulting in equality that looks at_lenghtto not behave as expected.See EqualityComparer.Default for information on the default
IEqualityComparerused by the Dictionary class.If you do not want to generally override
GetHashCodeandEqualson the class, or if you are unable to. There is an overload of the Dictionary constructor in which you can provide the specificIEqualityComparerto use.It is a simple interface to implement, but you do need to be careful that you respect the contract for
GetHashCodeor you can end up with unexpected behavior.to use it just go
If you want to use the default IEqualityComparer you need to provide roughly the same methods on MyObjectEqualityComparer. You can avoid overriding
object.Equals()if you implement IEquatable. However I would strongly discourage it because doing so can create some surprising behavior. You are better of overridingEqualsso that you have consistent behavior for all calls to Equals and have hashing that properly matches Equals. I have had to fix a bug in inherited code caused by a past developer only implementingIEquatable.