当使用IComparable的< T>比。的IComparer< T> [英] When to use IComparable<T> Vs. IComparer<T>

查看:188
本文介绍了当使用IComparable的< T>比。的IComparer< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

那么,他们都没有的非常的同样的事情,的IComparer&LT; T&GT; T&GT; 的是,其能够将自己与相同类型的其他实例比较类型实施<上的类型,其能够比较两个不同的物体,同时 IComparable的&所述实施/ P>

我倾向于使用 IComparable的&LT; T&GT; 的时候,我需要知道如何另一个实例涉及到实例。 的IComparer&LT; T&GT; 是分类收集有用的的IComparer&LT; T&GT; 表示比较之外

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.

这篇关于当使用IComparable的&LT; T&GT;比。的IComparer&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆