我试图找出这些接口,我需要实现的。他们都基本上做同样的事情。我什么时候会用一个比其他?
解决方案那么,他们都没有的非常的同样的事情,的IComparer< T>
T> 的是,其能够将自己与相同类型的其他实例比较类型实施
我倾向于使用 IComparable的< T>
的时候,我需要知道如何另一个实例涉及到这
实例。 的IComparer< T>
是分类收集有用的的IComparer< T>
表示比较之外
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?
解决方案Well they are not quite the same thing as IComparer<T>
is implemented on a type that is capable of comparing two different objects while IComparable<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 to this
instance. IComparer<T>
is useful for sorting collections as the IComparer<T>
stands outside of the comparison.