Skip to content

Commit

Permalink
Eliminate keySelector null-check in ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
deeprobin committed Jun 14, 2022
1 parent fbb3a74 commit 879421b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
30 changes: 22 additions & 8 deletions src/libraries/System.Linq/src/System/Linq/OrderBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static partial class Enumerable
/// This method compares elements by using the default comparer <see cref="Comparer{T}.Default"/>.
/// </remarks>
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> source) =>
OrderBy(source, static element => element);
new OrderedEnumerable<T, T>(source, static element => element, null, false, null);

/// <summary>
/// Sorts the elements of a sequence in ascending order.
Expand All @@ -42,7 +42,7 @@ public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> source) =>
/// If comparer is <see langword="null"/>, the default comparer <see cref="Comparer{T}.Default"/> is used to compare elements.
/// </remarks>
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> source, IComparer<T> comparer) =>
OrderBy(source, static element => element, comparer);
new OrderedEnumerable<T, T>(source, static element => element, comparer, false, null);

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
new OrderedEnumerable<TSource, TKey>(source, keySelector, null, false, null);
Expand All @@ -66,7 +66,7 @@ public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerabl
/// This method compares elements by using the default comparer <see cref="Comparer{T}.Default"/>.
/// </remarks>
public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> source) =>
OrderByDescending(source, static element => element);
new OrderedEnumerable<T, T>(source, static element => element, null, true, null);

/// <summary>
/// Sorts the elements of a sequence in descending order.
Expand All @@ -85,13 +85,27 @@ public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> sourc
/// If comparer is <see langword="null"/>, the default comparer <see cref="Comparer{T}.Default"/> is used to compare elements.
/// </remarks>
public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> source, IComparer<T> comparer) =>
OrderByDescending(source, static element => element, comparer);
new OrderedEnumerable<T, T>(source, static element => element, comparer, true, null);

public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
new OrderedEnumerable<TSource, TKey>(source, keySelector, null, true, null);
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
if (keySelector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}

return new OrderedEnumerable<TSource, TKey>(source, keySelector, null, true, null);
}

public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer) =>
new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, true, null);
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer)
{
if (keySelector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}

return new OrderedEnumerable<TSource, TKey>(source, keySelector, comparer, true, null);
}

public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ internal OrderedEnumerable(IEnumerable<TElement> source, Func<TElement, TKey> ke
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (keySelector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}

_parent = parent;
_keySelector = keySelector;
Expand Down

0 comments on commit 879421b

Please sign in to comment.