-1

Can someone please explain with example that I can understand about the difference between .Equals, IComparable and IComparer.

I was asked this in an interview.

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • `Equals` returns true/false if the two objects are equal (or the same reference depending on your implementation) IComparable/IComparer: http://stackoverflow.com/questions/5980780/difference-between-icomparable-and-icomparer – Chris Sinclair Jun 25 '12 at 16:15
  • Please search it on the internet you will get lots of resource answering your query here is one from me (I mean MS) http://support.microsoft.com/kb/320727 – HatSoft Jun 25 '12 at 16:15
  • oh my! such an easy question for interview. – DarthVader Jun 25 '12 at 16:16
  • Thanks Chris, but he was asking does Equals compare both strings and integers or he put this way, like reference type and value type.... He confused me :( – Jasmine Jun 25 '12 at 17:04
  • Dharnitski, HatSoft, thank you but really not the link I am expecting here. Also Dharnitski, MSDN I cant understand most thing (Like many small developers like me does)... Any document/tutorial is considered as good ONLY if it is reachable to even small minds :) Appreciate you folks though.... – Jasmine Jun 25 '12 at 17:06

3 Answers3

8

Well first off, on the surface, Equals is a method (present in every object), while IComparable and IComparer are interfaces.

Equals is present in any class and can be overriden to provide equality testing depending on the context of the class (it's a good practice to override GetHashCode as well). By default it just tests if objects are equal in memory which is not very useful. Equals (and GetHashCode) are usually given a different implementation in the context of searching or hashing.

Implementing IComparable is a more fine-grain way of comparison, as it provides the CompareTo method, which is a greater-than/less-than comparison as opposed to Equals which is simply a is-equal-or-not comparison. For example a binary search tree structure could benefit from this method.

IComparer is similar to IComparable, except that it works from the outside. It allows you to define a "neutral" object that is used for comparing two other objects without modifying them directly, which you need to do with IComparable.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • Uhmm Nice explanation, I wish I could have seen a small code to understand IComparable. Thank you, good explanation Tudor. – Jasmine Jun 25 '12 at 17:14
  • @Divine: You can find a code snipped at the end of the MSDN page: http://msdn.microsoft.com/en-us/library/system.icomparable.aspx – Tudor Jun 25 '12 at 17:17
  • Thank you Tudor, it helps me a lot :) Cheers – Jasmine Jun 25 '12 at 17:28
0

Equals is a method, when 2 other are interfaces. So look like the biggest difference.

More seriously - @ChrisSinclair gave you an answer in comments...

Equals returns true/false if the two objects are equal (or the same reference depending on your implementation) IComparable/IComparer: difference between IComparable and IComparer

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

.Equals() gives your class a way to test for equality against all other possible objects. This can be considered as the fallback for object equality. So this answers the question am I equivalent to the object passed in as a param.

IComparable provides for a way of comparing objects which can be ordered, possible uses include sorting. Implementing this interface puts the ordering logic into your class.

IComparer does pretty much the same as IComparable except the logic is contained in separate class.

Slugart
  • 4,535
  • 24
  • 32
  • THank you Slugart. Ok if IComparable does the same as IComparer, any good reason to see this in seperate class ? – Jasmine Jun 25 '12 at 17:09
  • 1
    Not coupling your comparison code to the class being compared allows you to provide multiple implementations of the ordering logic. Basic example is a ReverseComparer. – Slugart Jun 25 '12 at 18:15
  • Thank you Slugart, it helped me understand :) Cheers – Jasmine Jun 25 '12 at 18:28