Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know what happens if you do not implement iequtalable when using generic collections?

Tags:

c#

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?

like image 653
Jack Kada Avatar asked Mar 20 '10 09:03

Jack Kada


1 Answers

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.

like image 137
Guffa Avatar answered Sep 29 '22 07:09

Guffa