Skip to content

Commit

Permalink
Use compiler-generated delegate cache in "AssertCount"
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored Feb 27, 2023
1 parent 9b929f0 commit acad56c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 7 deletions.
7 changes: 1 addition & 6 deletions MoreLinq/AssertCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace MoreLinq

static partial class MoreEnumerable
{
static readonly Func<int, int, Exception> DefaultErrorSelector = OnAssertCountFailure;

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
/// </summary>
Expand All @@ -40,7 +38,7 @@ static partial class MoreEnumerable
/// </remarks>

public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource> source, int count) =>
AssertCount(source, count, DefaultErrorSelector);
AssertCount(source, count, static (cmp, count) => new SequenceException(FormatSequenceLengthErrorMessage(cmp, count)));

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
Expand Down Expand Up @@ -93,9 +91,6 @@ public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource
}
}

static Exception OnAssertCountFailure(int cmp, int count) =>
new SequenceException(FormatSequenceLengthErrorMessage(cmp, count));

internal static string FormatSequenceLengthErrorMessage(int cmp, int count) =>
$"Sequence contains too {(cmp < 0 ? "few" : "many")} elements when exactly {count:N0} {(count == 1 ? "was" : "were")} expected.";
}
Expand Down
1 change: 0 additions & 1 deletion MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ public static IEnumerable<TSource> Assert<TSource>(this IEnumerable<TSource> sou
[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
public static partial class AssertCountExtension
{

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
/// </summary>
Expand Down

0 comments on commit acad56c

Please sign in to comment.