dotnet/reactive · error · ArgumentNullException

Thrown when source is null (ArgumentNullException, param…

Error message

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

What it means

The obsolete single-overload MaxBy selects elements with the maximum key via a keySelector function. It validates arguments eagerly: a null source throws ArgumentNullException with paramName 'source'. This overload is marked [Obsolete]; MaxByWithTies is the recommended replacement.

Solutions

  1. Migrate to MaxByWithTies(source, keySelector), which is the supported API, and null-check the source first.
  2. Guard the call: if (rows == null) return default list; before invoking MaxBy.
  3. Coalesce the source with an empty collection, then handle the empty-result case (First will throw on empty).

Example fix

// before
var best = rows.MaxBy(r => r.Score); // rows may be null; also obsolete
// after
var best = rows is null ? [] : rows.MaxByWithTies(r => r.Score);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) return new List<TSource>();
var best = source.MaxBy(x => x.Key); // prefer MaxByWithTies

Type guard

static bool CanMaxBy<T, TKey>(IEnumerable<T>? source, Func<T, TKey>? keySelector) => source is not null && keySelector is not null;

Try / catch

try
{
    var best = source.MaxBy(x => x.Score);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    var best = new List<TSource>();
}

Prevention

When it happens

Trigger: Calling source.MaxBy(keySelector) where source is null, e.g. rows.MaxBy(r => r.Score) with rows == null. Compiles with an Obsolete warning; the null check throws before enumeration.

Common situations: Using the legacy MaxBy from older Ix-based code after upgrading, with a collection variable that can be null; copy-pasted query pipelines where an upstream operator returned null.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/MaxBy.cs:24

namespace System.Linq
{
    public static partial class EnumerableEx
    {
#if !(REFERENCE_ASSEMBLY && NET6_0_OR_GREATER)
        /// <summary>
        /// Returns the elements with the maximum key value by using the default comparer to compare key values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <typeparam name="TKey">Key type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
        /// <returns>List with the elements that share the same maximum key value.</returns>
        [Obsolete("Use MaxByWithTies to maintain same behavior with .NET 6 and later", false)]
        public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));

            return MaxBy(source, keySelector, Comparer<TKey>.Default);
        }

        /// <summary>
        /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <typeparam name="TKey">Key type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
        /// <param name="comparer">Comparer used to determine the maximum key value.</param>
        /// <returns>List with the elements that share the same maximum key value.</returns>
        [Obsolete("Use MaxByWithTies to maintain same behavior with .NET 6 and later", false)]
        public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
        {

View on GitHub (pinned to 94b5d5ab91)