什么是比较器< T>类? [英] What's the Comparer<T> class for?

查看:111
本文介绍了什么是比较器< T>类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么目的请问的Comparer< T> 类服务,如果你已经指定工具类型 IComparable的

What purpose does the Comparer<T> class serve if the type that you specify already implements IComparable?

如果我指定Comparer.Default和客户已经实现了IComparable的,那么为什么我会用比较器类?

If I specify Comparer.Default, and Customer already implements IComparable, then why would I use the Comparer class?

推荐答案

我觉得你的问题是,为什么有一个基类,只有似乎有这恰好是如果直接实现的接口你实现了相同的方法的一个有用的方法。如果我的理解是正确的我想你是对的,没有太多的好处,从派生的Comparer< T> ,而不是实施的IComparer< T> 直接除了基类为您提供了一个通用和非通用实现用一个重写的方法。

I think your question is why have a base class that only seems to have one useful method which happens to be the same method you'd implement if you implemented the interface directly. If I understood that correctly I guess you're right that there's not much benefit to deriving from Comparer<T> as opposed to implementing IComparer<T> directly except that the base class provides you with a generic and non-generic implementation with a single overridden method.

但如果你的问题是,为什么同时拥有的IComparer< T> IComparable的< T> ,然后
正如其他人所指出的,的Comparer< T> 允许您定义不同的方式来进行比较。它是策略设计图案的实现。这方面的一个很好的例子是各种 StringComparer 属性,如StringComparer.Ordinal,StringComparer.OrdinalIgnoreCase等,这可以让你以不同的字符串不同情况下该<$排序C $ C> IComparable的< T> 接口根本无法预料

But if your question is why have both IComparer<T> and IComparable<T>, then as others have indicated, Comparer<T> allows you to define different ways to perform the comparison. It is an implementation of the Strategy design pattern. A great example of this would be the various StringComparer properties such as StringComparer.Ordinal, StringComparer.OrdinalIgnoreCase, etc. This allows you to sort strings differently under different circumstances which the IComparable<T> interface simply cannot anticipate.

但除了能够重新定义的方式比较执行,有时提供外部比较器是你能做到这一点的唯一方法。例如,Windows窗体的ListView类,可以指定其排序逻辑的IComparer的。但ListViewItem的未实现IComparable。因此,没有一个知道怎么做了一个比较器的策略,ListViewItem的不可排序,因为它没有默认IComparable的实现。

But in addition to being able to re-define the way a comparison is performed, sometimes providing an external comparer is the only way you can do it. For example, the Windows Forms ListView class allows you to specify an IComparer for its sort logic. But ListViewItem does not implement IComparable. So without a comparer strategy that knows how to do it, ListViewItem is not sortable because it has no default IComparable implementation.

因此,在这一天结束,它只是一个扩展点,让您可以更灵活,当你不就是要排序的类型的作者(或需要多个排序策略。)

So at the end of the day, it's just another extensibility point that allows you more flexibility when you're not the author of the type that is to be sorted (or you need multiple sorting strategies.)



。编辑:在回答下面您的评论:如果我有一个实现的IComparer(我自己的类)类,这将让我排序的任何数量的属性(自定义排序),那么为什么会我招谁惹使用Comparer.Default

也许一个例子会有所帮助。比方说,你正在写检查,看看是否给定的值是一个范围之间的扩展方法。

Perhaps an example would help. Let's say you're writing an extension method that checks to see if a given value is between a range.

public static bool Between<T>(this T value, T minValue, T maxValue) {

    var comparer = Comparer<T>.Default;

    int c1 = comparer.Compare(value, minValue);
    int c2 = comparer.Compare(value, maxValue);

    return (c1 >= 0 && c2 <= 0);

}

在这种情况下,我不知道什么的类型T.它可以实现 IComparable的或者它可以实现 IComparable的< T> 或者它可以实现既不和异常会被抛出。这也让我可以轻松地添加过载这种方法,让自己的比较器来电通。但是的Comparer就派上用场了这里,因为它让我得到了可能或不可能实现一个通用的或者非泛型IComparable接口未知类型的默认比较器。

In this case, I don't know anything about the type T. It may implement IComparable or it may implement IComparable<T> or it may implement neither and an exception will be thrown. This also allows me to easily add an overload for this method that lets the caller pass in their own comparer. But Comparer comes in handy here because it lets me get a default comparer for an unknown type that may or may not implement a generic or non-generic IComparable interface.

这篇关于什么是比较器&LT; T&GT;类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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