Ray's playground

 

Item 31: Implement Ordering Relations with IComparable<T> and IComparer<T>(Effective C#)

IComparable and IComparer are the standard mechanisms for providing ordering relations for your types. IComparable should be used for the most natural ordering. When you implement IComparable, you should overload the comparison operators (<, >, <=, >=) consistently with our IComparable ordering. IComparable.CompareTo() uses System.Object parameters, so you should also provide a type-specific overload of the CompareTo() method. IComparer can be used to provide alternative orderings or can be used when you need to provide ordering for a type that does not provide it for you.

posted on 2011-02-21 20:55  Ray Z  阅读(176)  评论(0编辑  收藏  举报

导航