dotnet/reactive · error · ArgumentNullException

Thrown when comparer is null (ArgumentNullException, param…

Error message

Thrown when comparer is null (ArgumentNullException, param name: comparer)

What it means

The comparer-based Max operator requires a non-null IComparer<TSource> to order elements. Passing a null comparer throws ArgumentNullException with paramName 'comparer' immediately at the call site, before enumeration.

Solutions

  1. Pass a concrete comparer such as Comparer<TSource>.Default when no custom ordering is needed.
  2. Null-check or default the comparer before the call: comparer ??= Comparer<TSource>.Default;
  3. Fix the comparer's construction/registration (DI binding, factory) so it is never null.

Example fix

// before
var max = data.Max(cfg.Comparer); // Comparer not configured -> null
// after
var max = data.Max(cfg.Comparer ?? Comparer<T>.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (comparer is null) comparer = Comparer<TSource>.Default;
var max = source.Max(comparer);

Type guard

static IComparer<T> NonNull<T>(IComparer<T>? c) => c ?? Comparer<T>.Default;

Try / catch

try
{
    var max = source.Max(comparer);
}
catch (ArgumentNullException ex) when (ex.ParamName == "comparer")
{
    var max = source.Max(); // fall back to default comparison
}

Prevention

When it happens

Trigger: Calling source.Max(comparer) where the comparer argument is null, e.g. data.Max(null), or passing a comparer field/property that was never initialized.

Common situations: Storing a comparer in configuration/DI and getting null when it is not registered; conditionally constructing a comparer that ends up null; writing generic code that forwards a possibly-null comparer parameter.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Max.cs:25

namespace System.Linq
{
    public static partial class EnumerableEx
    {

#if !(REFERENCE_ASSEMBLY && NET6_0_OR_GREATER)
        /// <summary>
        /// Returns the maximum value in the enumerable sequence by using the specified comparer to compare values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="comparer">Comparer used to determine the maximum value.</param>
        /// <returns>Maximum value in the sequence.</returns>
        public static TSource Max<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

            return MaxByWithTies(source, x => x, comparer).First();
        }
#endif
    }
}

View on GitHub (pinned to 94b5d5ab91)