A better implementation could be:
public bool Equals(MyType other)
{
// if 'other' is a null reference, or if 'other' is more derived or less derived
if ((object)other == (object)null || other.GetType() != GetType())
return false;
// OK, check members (assuming 'Id' has a type that makes '==' a wise choice)
return Id == other.Id;
}
public override bool Equals(object obj)
{
// call to other overload
return Equals(obj as MyType);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
You can mark the class as implementing IEquatable<MyType>
in that case (but it will work even without that).
Regarding GetHashCode
: Always remember to override it. You should have seen a compiler warning that it was problematic to override Equals(object)
without overriding GetHashCode
. Never keep the code return base.GetHashCode()
in the override (assuming the base class is System.Object
). Either give it a try and implement something based on the members that participate in Equals
. If you do not think GetHashCode
will actually be used in your case, say:
public override int GetHashCode()
{
throw new NotSupportedException("We don't have GetHashCode, sorry");
}
If you absolutely know that you will only be using List<>.Contains
, and not e.g. Dictionary<,>
, HashSet<>
and not Linq's Distinct()
, etc. etc., it could work with GetHashCode()
simply throwing.
IComparable<MyType>
is not needed unless you sort List<MyType>
or MyType[]
, or you use Linq's OrderBy
with MyType
, or you use SortedDictionary<,>
, SortedSet<>
.
Overloading operator ==
is not needed for these uses.