dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

The comparer-based Min<TSource>(source, comparer) extension validates that the source sequence is non-null and throws ArgumentNullException('source') first. Min needs a real sequence to scan; null is treated as a programming error rather than an empty result.

Solutions

  1. Return/pass an empty sequence (Enumerable.Empty<T>()) instead of null.
  2. Coalesce at the call site: (source ?? Enumerable.Empty<TSource>()).Min(comparer).
  3. Fix the upstream producer so it never yields null sequences.

Example fix

// before
var min = GetValues().Min(Comparer<int>.Default); // GetValues() returned null
// after
var min = (GetValues() ?? Enumerable.Empty<int>()).Min(Comparer<int>.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new InvalidOperationException("Sequence required.");
var min = source.Min(comparer);

Type guard

static bool HasValues<T>(IEnumerable<T>? s) => s is not null;

Try / catch

try { min = source.Min(comparer); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { min = default; }

Prevention

When it happens

Trigger: Calling source.Min(comparer) where source is null — e.g. a method returning null instead of an empty sequence, an unassigned field, or the result of a failed lookup passed directly into Min.

Common situations: Repository/parse functions that return null instead of empty collections; nullable fields fed into extension methods;LINQ chains where an earlier custom operator produced null.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Min.cs:22

using System.Collections.Generic;

namespace System.Linq
{
    public static partial class EnumerableEx
    {
#if !(REFERENCE_ASSEMBLY && NET6_0_OR_GREATER)
        /// <summary>
        /// Returns the minimum 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 minimum value.</param>
        /// <returns>Minimum value in the sequence.</returns>
        public static TSource Min<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 MinByWithTies(source, x => x, comparer).First();
        }
#endif
    }
}

View on GitHub (pinned to 94b5d5ab91)