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;
}
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.?
Equals
does change how==
behaves" - that should probably say "does not change"? – Rudin