我在 .Net 4.5 中创建了一个比较器:
public static Comparer<Room> RoomComparer =
Comparer<Room>.Create((a, b) => a.RoomId.CompareTo(b.RoomId))
我如何在 c# .NET 4.0 中创建比较器?
最佳答案
您可以使用以下
public static class Comparer
{
public static Comparer<T> Create<T>(Comparison<T> comparison)
{
if (comparison == null) throw new ArgumentNullException("comparison");
return new ComparisonComparer<T>(comparison);
}
private sealed class ComparisonComparer<T> : Comparer<T>
{
private readonly Comparison<T> comparison;
public ComparisonComparer(Comparison<T> comparison)
{
this.comparison = comparison;
}
public override int Compare(T x, T y)
{
return comparison(x, y);
}
}
}
像这样
public static Comparer<Room> RoomComparer =
Comparer.Create<Room>((a, b) => a.RoomId.CompareTo(b.RoomId))
关于c# - .NET 4 中的比较器<T>.Create,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32901435/