当使用IComparable的< T>比。的IComparer< T>LT、IComparable、IComparer、GT

2023-09-02 01:31:29 作者:沉默游走

我试图找出这些接口,我需要实现的。他们都基本上做同样的事情。我什么时候会用一个比其他?

解决方案

那么,他们都没有的非常的同样的事情,的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.

主打颜值和影像的真我GT大师系列,还是性价比手机新风向标

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.