10

I've found this code in here:

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
    if (source == null) throw new ArgumentNullException(nameof(source));
    if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
    return _(); IEnumerable<TSource> _()
    {
        var knownKeys = new HashSet<TKey>(comparer);
        foreach (var element in source)
        {
            if (knownKeys.Add(keySelector(element)))
                yield return element;
        }
    }
}

At first I didn't understand this part return _(); IEnumerable<TSource> _(), but I realized that it's a local function called and declared in the same line. It was done here.

My question is: is there any advantage over simply inlining that code?

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
    if (source == null) throw new ArgumentNullException(nameof(source));
    if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
    var knownKeys = new HashSet<TKey>(comparer);
    foreach (var element in source)
    {
        if (knownKeys.Add(keySelector(element)))
            yield return element;
    }
}

I'd say the current version is more verbose and has more indentation, so what is the advantage? Is just a matter of taste?

Tao Gómez Gil
  • 2,228
  • 20
  • 36
  • 4
    [Very relevant](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions#local-functions-and-exceptions) – Sweeper Jun 25 '21 at 13:37
  • Before local methods this would have been implemented with 2 methods, the first method would call the second method after the null checks. The introduction of local methods allows all of the implementation to be inside of one method now. – juharr Jun 25 '21 at 14:36
  • Does this answer your question? [Why would one define a local function in C# just to call it right away (in context of IEnumerable)](https://stackoverflow.com/questions/54533438/why-would-one-define-a-local-function-in-c-sharp-just-to-call-it-right-away-in) – GSerg Jun 25 '21 at 21:56
  • Does this answer your question? [Local functions benefits with async methods](https://stackoverflow.com/q/59823222/11683) – GSerg Jun 25 '21 at 21:56

1 Answers1

15

The version with the local method will throw immediately if source or keySelector is null, because it's not implemented with an iterator block.

The "inlined" version uses an iterator block, so none of the code - including the validation - will execute until the calling code starts to iterate over the returned IEnumerable<TSource>.

In general, eager validation makes it easier to spot and understand errors: the stack trace is clearer, and there's no delay between "making the broken call" and "seeing the failure".

The same approach can also be used to write task-returning methods that fail eagerly: write a "regular" task-returning method that calls an async local method after performing validation.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    "making the broken call" and "seeing the failure" don't need quotation marks around them. (Ironically, they do in this comment) – user253751 Jun 26 '21 at 00:09