Is there any reasonable scenario for consisting of iomparer <T>?
I have never written stateful IComparer<T>with a standard constructor. All the standard library implementations that I tested in Reflector are stateless as well. Therefore, I would like to suggest that I can cache freely IComparer<T>as follows:
PriorityQueue<TPriority, TComparer> where TComparer : IComparer<TPriority>, new()
{
private static TComparer _comparer = new TComparer();
public PriorityQueue() {...}
...
}
instead
PriorityQueue<TPriority>
{
private IComparer<TPriority> _comparer;
public PriorityQueue(IComparer<TPriority> comparer) {
_comparer = comparer;
...
}
...
}
So, the question is: have you ever written / seen IComparer<T>for which it would break? If so, how common is this?
EDIT: , , , . , / . , , node! , IComparable<T> .
+3
3
, ; ... ; , . :
PriorityQueue<TPriority>
{
private IComparer<TPriority> _comparer;
public PriorityQueue(IComparer<TPriority> comparer) {
_comparer = comparer;
...
}
public PriorityQueue() : this(Comparer<T>.Default) {}
}
; , - LINQ-... , - :
public static class ProjectionComparer<TSource>
{
public static IComparer<TSource> CompareBy<TValue>(
Func<TSource, TValue> selector)
{
return CompareBy<TValue>(selector, Comparer<TValue>.Default);
}
public static IComparer<TSource> CompareBy<TValue>(
Func<TSource, TValue> selector,
IComparer<TValue> comparer)
{
return new ProjectionComparerItem<TValue>(
selector, Comparer<TValue>.Default);
}
class ProjectionComparerItem<TValue> : IComparer<TSource>
{
private readonly IComparer<TValue> comparer;
private readonly Func<TSource, TValue> selector;
public ProjectionComparerItem(
Func<TSource, TValue> selector,
IComparer<TValue> comparer)
{
this.selector = selector;
this.comparer = comparer;
}
public int Compare(TSource x, TSource y)
{
// TODO: some null stuff...
return comparer.Compare(selector(x), selector(y));
}
}
}
:
IComparer<Customer> comparer = ProjectionComparer<Customer>
.CompareBy(cust => cust.Name);
" ".
+3
:
private class PriorityQueueImpl<TPriority, TComparer> where TComparer : IComparer<TPriority> {
// all methods accept a TComparer
// generic in TComparer to avoid boxing for struct TComparers and permit inlining for sealed TComparers
}
public struct PriorityQueue<TPriority, TComparer> where TComparer : IComparer<TPriority> {
private readonly PriorityQueueImpl<TPriority, TComparer> _impl;
private readonly TComparer _comparer;
// methods delegate to _impl
}
, , .
0