问题比较项目实施IComparable的 [英] Problem comparing items implementing IComparable
问题描述
我工作的地方特定选择找到分项的扩展方法。下面的代码
I am working on a extension method where it finds the min item by specific selector. Below the code
public static T MinBy<T, K>(this IEnumerable<T> src, Func<T, K> selector) where K : struct, IComparable, IConvertible
{
var min = default(K);
T minItem = default(T);
foreach (var item in src)
{
var current = selector(item);
if (current < min)
{
min = current;
minItem = item;
}
}
return minItem;
}
这使误差错误操作 <'不能应用于类型的操作数'K'和'K'
。但我有指定通用约束K在 struct和IComparable的
。我相信所有的数字数据类型可以是满意。
It gives error Error Operator '<' cannot be applied to operands of type 'K' and 'K'
. But i have specified the generic constraint K should be Struct and IComparable
. I believe all the numeric data type can be satisfied with this.
那么怎么来的,这是一个无效的操作?
Then how come this is a invalid operation.?
推荐答案
IComparable的
不会(也不能)说,关于运营商什么。您应该使用:
IComparable
doesn't (and can't) say anything about operators. You should be using:
if (current.CompareTo(min) < 0)
运营商是静态的,只有永远的重载的而不是覆盖的。可以不需要接口内操作员,以及一个方法的存在不会奇迹般地改变操作者将执行的操作。 (例如,覆盖等于
并不会改变 ==
的行为。)
Operators are static, and only ever overloaded rather than overridden. You can't require operators within interfaces, and the presence of a method doesn't magically change what an operator will do. (For example, overriding Equals
does not change how ==
behaves.)
您还应该注意到,作为您的约束只有非泛型 IComparable的
界面会谈,你会在每一个操作进行拳击。我建议你改变约束 IComparable的< K>
来代替。 (或者删除约束,只是使用的Comparer< K> .DEFAULT
如马克的建议)
You should also note that as your constraint only talks about the nongeneric IComparable
interface, you're going to be boxing at every operation. I would suggest you change the constraint to IComparable<K>
instead. (Or drop the constraint and just use Comparer<K>.Default
as Marc suggested.)
其他的一些评论关于你的方法:
Some other comments about your method:
- 如果所有关键值比默认值多为
K
(如:K = int和所有按键均为正),那么你不会找到一个项目 - 您可能希望它接受一个特定的
IComparare< K>
(但只有当你放弃可比约束) - 有没有真正需要约束
K
来的值类型。如果我想用字典顺序最早的名字吗? - 如果没有的元素,它会返回
T $ C $默认值找人C>;以配合LINQ的其余部分,我建议抛出
出现InvalidOperationException
- 我会建议使用
TSource
和TKEY的
作为类型参数与LINQ更一致
- If all of the key values are more than the default value for
K
(e.g. K=int and all the keys are positive) then you won't find an item - You may wish to have an overload which accepts a particular
IComparare<K>
(but only if you drop the comparable constraint) - There's no real need to constrain
K
to a value type. What if I wanted to find the person with the lexicographically earliest name? - If there are no elements, it will return the default value for
T
; to fit in with the rest of LINQ I would suggest throwingInvalidOperationException
- I would suggest using
TSource
andTKey
as the type parameters to be more consistent with LINQ
您可能想要看的 MoreLINQ MinBy实施的作为替代。 (纵观一遍,我不知道这是一个好主意,我们需要比较器
非空;它或许应该使用默认的比较同样的方式正常LINQ确实,如果比较器为null。)
You may want to look at the MoreLINQ MinBy implementation as an alternative. (Looking over that again, I'm not sure it's a good idea for us to require that comparer
is non-null; it should probably use the default comparer in the same way as normal LINQ does if the comparer is null.)
这篇关于问题比较项目实施IComparable的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!