优势/不同的实现方式的对象比较劣势 [英] Advantages/Disadvantages of different implementations for Comparing Objects
问题描述
这个问题涉及到两个不同的基本上是相同的code实现。
This questions involves 2 different implementations of essentially the same code.
首先,利用委托创建一个比较方法,其可以当排序对象的集合被用作参数:
First, using delegate to create a Comparison method that can be used as a parameter when sorting a collection of objects:
class Foo
{
public static Comparison<Foo> BarComparison = delegate(Foo foo1, Foo foo2)
{
return foo1.Bar.CompareTo(foo2.Bar);
};
}
我用上面时,我想有排序美孚的集合的方式以不同的方式比我的CompareTo函数提供了对象。例如:
I use the above when I want to have a way of sorting a collection of Foo objects in a different way than my CompareTo function offers. For example:
List<Foo> fooList = new List<Foo>();
fooList.Sort(BarComparison);
二,使用的IComparer:
Second, using IComparer:
public class BarComparer : IComparer<Foo>
{
public int Compare(Foo foo1, Foo foo2)
{
return foo1.Bar.CompareTo(foo2.Bar);
}
}
我用上面时,我想要做一个二进制搜索在美孚对象的集合Foo对象。例如:
I use the above when I want to do a binary search for a Foo object in a collection of Foo objects. For example:
BarComparer comparer = new BarComparer();
List<Foo> fooList = new List<Foo>();
Foo foo = new Foo();
int index = fooList.BinarySearch(foo, comparer);
我的问题是:
My questions are:
- 什么是所有这些实现的优点和缺点?
- 什么是一些更多的方法来把每一实现的优势在哪里?
- 有没有办法将这些实现结合起来,我不需要重复code这样的方式?
- 我可以同时实现二进制搜索,并只使用1这些实现的? 排序替代集合
- What are the advantages and disadvantages of each of these implementations?
- What are some more ways to take advantage of each of these implementations?
- Is there a way to combine these implementations in such a way that I do not need to duplicate the code?
- Can I achieve both a binary search and an alternative collection sort using only 1 of these implementations?
推荐答案
大概在接受最大的优势比较&LT; T&GT;
而不是一个的IComparer&LT; T&GT;
是写匿名方法的能力。如果我有,比方说,一个名单,其中,MyClass的&GT;
,其中 MyClass的
包含一个 ID
属性,它应该用于排序,我可以这样写:
Probably the biggest advantage to accepting a Comparison<T>
as opposed to an IComparer<T>
is the ability to write anonymous methods. If I have, let's say, a List<MyClass>
, where MyClass
contains an ID
property that should be used for sorting, I can write:
myList.Sort((c1, c2) => c1.ID.CompareTo(c2.ID));
这是很多不必写出更方便的整个的IComparer&LT; MyClass的&GT;
实施
我不知道,接受了的IComparer&LT; T&GT;
真有什么大的优势,除了与传统code(包括.NET Framework类)的兼容性。该的Comparer&LT; T&GT; .DEFAULT
属性仅对基本类型真正有用的;一切通常需要额外的工作来code反对。
I'm not sure that accepting an IComparer<T>
really has any major advantages, except for compatibility with legacy code (including .NET Framework classes). The Comparer<T>.Default
property is only really useful for primitive types; everything else usually requires extra work to code against.
要避免code复制,当我需要用的IComparer&LT工作; T&GT;
,有一件事我通常做的是建立一个通用的比较器,像这样的:
To avoid code duplication when I need to work with IComparer<T>
, one thing I usually do is create a generic comparer, like this:
public class AnonymousComparer<T> : IComparer<T>
{
private Comparison<T> comparison;
public AnonymousComparer(Comparison<T> comparison)
{
if (comparison == null)
throw new ArgumentNullException("comparison");
this.comparison = comparison;
}
public int Compare(T x, T y)
{
return comparison(x, y);
}
}
这使得写作code,如:
This allows writing code such as:
myList.BinarySearch(item,
new AnonymousComparer<MyClass>(x.ID.CompareTo(y.ID)));
这不完全pretty的,但它可以节省一些时间。
It's not exactly pretty, but it saves some time.
另一个有用的类我是这个:
Another useful class I have is this one:
public class PropertyComparer<T, TProp> : IComparer<T>
where TProp : IComparable
{
private Func<T, TProp> func;
public PropertyComparer(Func<T, TProp> func)
{
if (func == null)
throw new ArgumentNullException("func");
this.func = func;
}
public int Compare(T x, T y)
{
TProp px = func(x);
TProp py = func(y);
return px.CompareTo(py);
}
}
,你可以写code专为的IComparer&LT; T&GT;
为:
myList.BinarySearch(item, new PropertyComparer<MyClass, int>(c => c.ID));
这篇关于优势/不同的实现方式的对象比较劣势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!