dotnet/reactive · error · ArgumentNullException

ArgumentNullException: elementSelector

Error message

ArgumentNullException: elementSelector

What it means

The GroupBy(source, keySelector, elementSelector) overload throws ArgumentNullException when elementSelector is null. The element selector projects each item into the group's element type and is mandatory in this overload. The check is performed synchronously before any subscription happens.

Solutions

  1. Supply a non-null element projection such as x => x to keep elements unchanged
  2. Switch to the GroupBy(source, keySelector) overload when no projection is needed
  3. Default null selectors to x => x in wrapper code

Example fix

// before
var grouped = source.GroupBy(x => x.Key, null);
// after
var grouped = source.GroupBy(x => x.Key, x => x);
Defensive patterns

Strategy: validation

Validate before calling

if (elementSelector == null) throw new ArgumentNullException(nameof(elementSelector));

Type guard

bool IsValidSelector<TSource,TElement>(Func<TSource,TElement> f) => f is not null;

Try / catch

try { var g = source.GroupBy(k, elementSelector); } catch (ArgumentNullException ex) when (ex.ParamName == "elementSelector") { /* fall back to identity projection */ }

Prevention

When it happens

Trigger: Calling the three-argument GroupBy overload with null as elementSelector, e.g. GroupBy(source, x => x.Key, null); use the two-argument overload if no element projection is needed.

Common situations: Devs upgrading from the two-argument overload who forget to supply the projection; conditional projection logic that leaves the Func null; generic helper methods forwarding a null selector parameter.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/9ea0052cbc54c32c. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs:81

            if (capacity < 0)
                throw new ArgumentOutOfRangeException(nameof(capacity));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(
                source,
                (keySelector, capacity, comparer),
                static (source, state, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, state.keySelector, state.capacity, state.comparer)));
        }

        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (elementSelector == null)
                throw new ArgumentNullException(nameof(elementSelector));

            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TElement>>.From(
                source,
                (keySelector, elementSelector),
                static (source, state, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, state.keySelector, state.elementSelector)));
        }

        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (elementSelector == null)
                throw new ArgumentNullException(nameof(elementSelector));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

View on GitHub (pinned to 94b5d5ab91)