進哥的布拉格

Chin Gooole's Blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
Quoted from http://bytes.com/topic/c-sharp/answers/230884-icomparable-vs-icomparer
Any class that implements IComparable is *comparable*. That is, you can
compare an instance of that class with another instance in a manner that
you define.

OTOH, a class that implements IComparer is a... well, a comparer (Java
people call this a Comparator), i.e.: a class that imposes ordering on a
set of objects.

When you want to sort an unsorted array of objects of the same class,
you can either make the class implement IComparable, or you can
implement IComparer in a separate class, and pass that to Array.Sort().
Of course if the comparison is done based on a *private* member of the
class, IComparable is more appropriate, since having your IComparator
implementation be able to access its private members may not be
possible/appropriate.
posted on 2009-10-31 17:36  進哥  阅读(194)  评论(0编辑  收藏  举报