IComparer or IComparable interfaces...

I don't understand when one should implement the IComparer or IComparable interface or really the difference between the two? They both return int's based on whether one is greater, equal or less than the other.
dotnet22Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Farshid-ZakerConnect With a Mentor Commented:
hi dotnet22,
When you implement IComparable interface in a class, objects of it got the ability to compare themselves to any other object of the same class. That is a generic interface:
myObject.CompareTo(otherObject);

Classes that have implemented IComparer, just can compare any two other objects and return the result. They are not stateful objects and just COMPARE! IComparer objects are mostly used in Array.Sort or Array.BinarySearch methods as the last parameter.
There are 3 classes which has implemented it and ready for use in dotnet framework, the most famous one is Comparer. The two others are CaseInsensitiveComparer and KeysConverter.

Thanks,
Farshid
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.