views:

208

answers:

4

If I have a (reference - does it matter?) type MyType which does not override the Equals method, what heuristics will be used when determining if an ICollection<MyType> contains a given instance of the type?

What's the best way to use my own heuristics (e.g. check for the equality of the Id property value)?

+3  A: 

Because your type doesn't override Equals, the default implementation of Equals will be used, i.e. reference equality. So Contains will be true if the collection contains that very instance.

To use your own comparison, implement IEqualityComparer<T> (e.g. to compare the Ids) and pass an instance of your comparer into the Contains method. (This assumes you are able to use LINQ extensions, as the "native" ICollection<T>.Contains method doesn't have the IEqualityComparer overload.)

itowlson
to the point and complete. thanks
Ben Aston
out of interest are value types treated equally, and why not simply override the equals method?
Ben Aston
Value types are considered equal if all of their fields are equal. Of course, you can override Equals on a value type just as on a reference type, and can write a custom IEqualityComparer for a value type just as for a reference type.
itowlson
+1  A: 

It's not defined by ICollection<T>- different implementations can use different methods. From MSDN:

Implementations can vary in how they determine equality of objects; for example, List<T> uses Comparer<T>.Default, whereas Dictionary<TKey, TValue> allows the user to specify the IComparer<T> implementation to use for comparing keys

In most cases it will just compare the references, but you should check the documentation for the specific ICollection<T> you are using.

Mark Byers
A: 

AFAIK, the reference (address in memory) will be compared...

If the reference is not good enough for you then you might compare the ID, as you've mentioned.

Michael Kessler
A: 

According to MSDN:

Implementations can vary in how they determine equality of objects; for example, List<(Of <(T>)>) uses Comparer<(Of <(T>)>)..::.Default, whereas Dictionary<(Of <(TKey, TValue>)>) allows the user to specify the IComparer<(Of <(T>)>) implementation to use for comparing keys.

The best way to do it on your own is to use the overload that takes an IEqualityComparer<T>

public class MyComparer : IEqualityComparer<MyType>
{
    public bool Equals(MyType x, MyType y)
    {
        return x.Id == y.Id;
    }

    public int GetHashCode(MyType obj)
    {
        return obj.Id.GetHashCode();
    }
}
BFree
I hate that MSDN formats like that by default when you copy + paste. I just fixed mine up too. :-s
Mark Byers