dotnet/reactive · error · ArgumentNullException

comparer

Error message

comparer

What it means

The three-argument sync MaxBy overload requires an IComparer<TKey> to order keys. A null comparer is rejected with ArgumentNullException before subscription so the failure happens at call time, not during enumeration.

Solutions

  1. Pass Comparer<TKey>.Default or Comparer<TKey>.Create(...) explicitly
  2. Use the MaxBy(source, keySelector) overload which defaults to Comparer<TKey>.Default
  3. Ensure any comparer factory/registry returns a non-null instance

Example fix

// before
await source.MaxBy(x => x.Score, null);
// after
await source.MaxBy(x => x.Score, Comparer<int>.Default);
Defensive patterns

Strategy: validation

Validate before calling

var cmp = comparer ?? Comparer<TKey>.Default;
// then call: source.MaxBy(keySelector, cmp)

Type guard

bool HasComparer<TKey>(IComparer<TKey> cmp) => cmp is not null;

Try / catch

try
{
    result = source.MaxBy(keySelector, comparer);
}
catch (ArgumentNullException ex) when (ex.ParamName == "comparer")
{
    result = source.MaxBy(keySelector); // fall back to default comparer
}

Prevention

When it happens

Trigger: Calling AsyncObservable.MaxBy(source, keySelector, comparer) with comparer == null (e.g. an unset comparer field or a GetComparer() that returned null).

Common situations: DI/config supplies an optional comparer that is absent; a dictionary lookup for a comparer returns null; misuse of the overload instead of the comparer-less one.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/MaxBy.cs:32

            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));

            return CreateAsyncObservable<IList<TSource>>.From(
                source,
                keySelector,
                static (source, keySelector, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxBy(observer, keySelector)));
        }

        public static IAsyncObservable<IList<TSource>> MaxBy<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return CreateAsyncObservable<IList<TSource>>.From(
                source,
                (keySelector, comparer),
                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxBy(observer, state.keySelector, state.comparer)));
        }

        public static IAsyncObservable<IList<TSource>> MaxBy<TSource, TKey>(IAsyncObservable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));

            return CreateAsyncObservable<IList<TSource>>.From(
                source,
                keySelector,
                static (source, keySelector, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxBy(observer, keySelector)));

View on GitHub (pinned to 94b5d5ab91)