5

I asked a question here : When To Use IEquatable And Why about using IEquatable.

From the msdn:

The IEquatable(T) interface is used by generic collection objects such as Dictionary(TKey, TValue), List(T), and LinkedList(T) when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove.

If you dont implement that interface what exactly happens?? Exception / default object equals / ref equals?

Community
  • 1
  • 1
Jack Kada
  • 24,474
  • 29
  • 82
  • 106

3 Answers3

4

If you don't implement equality comparison, the default implementation will be used.

If the key is a reference type, reference comparision is used. Unless you keep the key objects so that you can use them when retrieving items from the collection, they are lost. Creating a new key object from the same data gives you an object with a difference reference, so it will miss the item you are looking for.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Here's one example:

public class Foo : IEquatable<Foo>
{
    public string Bar { get; set; }

    public bool Equals(Foo other)
    {
        return string.Equals(other.Bar, Bar);
    }
}

class Program
{

    public static void Main(string[] args)
    {
        var foos = new List<Foo>(new[] 
        { 
            new Foo { Bar = "b1" },
            new Foo { Bar = "b2" },
            new Foo { Bar = "b3" },
        });

        var foo = new Foo { Bar = "b2" };
        Console.WriteLine(foos.IndexOf(foo)); // prints 1
    }
}

Now comment the IEquatable<Foo> implementation and run the program again.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Interresting that the accepted answer doesn't actually contain the answer... ;) – Guffa Mar 20 '10 at 10:22
  • @Guffa, sometimes an example is worth better than thousand words. Please don't get me wrong, I think that your answer is excellent, it clearly explains the theory of how `IEquatable` works. It's just that when you give an example people can play with they can see how it works in practice. – Darin Dimitrov Mar 20 '10 at 10:31
  • Is there a special point to omitting the result of the example? – Guffa Mar 20 '10 at 14:57
2

It's a bit incorrect to say that Dictionary<TKey,TValue> uses IEquatable<T> to compare TKey values. It actually uses an IEquatilyComparer<T> to compare values.

When one is not explicitly provided though it will use EqualityComparer<TKey>.Default to generate the comparer. This static property has a heuristic to determine which is the best way to compare TKey values. If the type TKey implements IEqutable<TKey> this will be used. If it's not though it will fall back to using the .Equals method directly.

So if you don't implement IEquatable<T> and don't provide an explicity IEqualityComparer<T> then Dictionary<TKey,TValue> will rely on normal .Equals.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454