I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
Use
IComparable<T>
when the class has an intrinsic comparison.Use
IComparer<T>
when you want a comparison method other than the class' intrinsic comparison, if it has one.Simple Explanation via a story
High school basketball. It's a school yard pick for the teams. I want to get the tallest/best/fastest folks on my team. What do I do?
IComparer Interface - Compare two people separate people
Compare(Fred, John)
and it spits out who's better.What about IComparable? - Compare yourself with someone else
Have you been on FB recently? You see other folks doing cool things: travelling the world, creating inventions, while I'm doing something not quite as cool - well what we are doing is making use of the IComparable interface.
What about the Comparer Class?
The Comparer class is an abstract base class which implements the IComparer interface. You should derive from this class to have a concrete implementation. anyways, Microsoft recommends that you DO use the Comparer class rather than implement the IComparer interface:
Summary
Hope the stories help you remember.
IComparable says an object can be compared with another. IComparer is an object that can compare any two items.
Well they are not quite the same thing as
IComparer<T>
is implemented on a type that is capable of comparing two different objects whileIComparable<T>
is implemented on types that are able to compare themselves with other instances of the same type.I tend to use
IComparable<T>
for times when I need to know how another instance relates tothis
instance.IComparer<T>
is useful for sorting collections as theIComparer<T>
stands outside of the comparison.IComparer is a interface which is used to sort the Array, this interface will force the class to implement the Compare(T x,T y) method, which will compare the two objects. The instance of the class which implemented this interface is used in the sorting of the Array.
IComparable is a interface is implemented in the type which needs to compare the two objects of the same type, This comparable interface will force the class to implement the following method CompareTo(T obj)
IEqualityComparer is a interface which is used to find the object whether it is Equal or not, Now we will see this in a sample where we have to find the Distinct of a Object in a collection. This interface will implement a method Equals(T obj1,T obj2)
Now we take a Example we have a Employee class , based on this class we have to create a Collection. Now we have the following requirements.
Sort the Array using Array class 2. Need an collection using Linq : Remove the Duplicate, Order by higher to lower, Remove one employee id
public class EmployeeIdSorter : IComparer { public int Compare(Employee x, Employee y) { if (x.Id < y.Id) return 1; else if (x.Id > y.Id) return -1; else return 0; } }
For more info refere below http://dotnetvisio.blogspot.in/2015/12/usage-of-icomparer-icomparable-and.html
It all depends on whether your type is mutable or not. You should only implement IComparable on non-mutable types. Note that if you implement IComparable, you must override Equals, along with the ==, !=, < and > operators (see Code Analysis warning CA1036).
Quoting Dave G from this blog post: