dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'sources')

Error message

Value cannot be null. (Parameter 'sources')

What it means

The Concat overload Concat<TSource>(this IEnumerable<IEnumerable<TSource>> sources) throws ArgumentNullException with parameter name 'sources' when the outer sequence of sequences is null. Ix.NET validates arguments eagerly so the null is reported at the call site rather than during deferred enumeration. Pass an empty sequence to concatenate nothing.

Solutions

  1. Ensure the outer sequence is non-null; use Enumerable.Empty<IEnumerable<TSource>>() for no input.
  2. Coalesce at the call site: (sources ?? Enumerable.Empty<IEnumerable<T>>()).Concat().
  3. Fix the upstream producer to return an empty collection instead of null.
  4. Enable nullable reference types so the compiler warns before the call.

Example fix

// before
var result = ((IEnumerable<IEnumerable<int>>)null).Concat();
// after
var result = (sources ?? Enumerable.Empty<IEnumerable<int>>()).Concat();
Defensive patterns

Strategy: validation

Validate before calling

if (sources is null)
    throw new ArgumentNullException(nameof(sources));
// or coalesce:
sources ??= Enumerable.Empty<IEnumerable<TSource>>();

Type guard

static bool IsUsableSources<TSource>(IEnumerable<IEnumerable<TSource>> s) => s != null;

Try / catch

try
{
    var result = sources.Concat();
}
catch (ArgumentNullException ex) when (ex.ParamName == "sources")
{
    var result = Enumerable.Empty<TSource>();
}

Prevention

When it happens

Trigger: Calling sources.Concat() with a null outer enumerable, e.g. a batch list variable that was never initialized.

Common situations: A factory or deserializer returning null for an empty collection; a nullable field passed directly as the outer sequence; pipeline code where an earlier step produced null.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Concat.cs:20

// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Collections.Generic;

namespace System.Linq
{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Concatenates the input sequences.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="sources">Source sequences.</param>
        /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
        public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
        {
            if (sources == null)
                throw new ArgumentNullException(nameof(sources));

            return ConcatCore(sources);
        }

        /// <summary>
        /// Concatenates the input sequences.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="sources">Source sequences.</param>
        /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
        public static IEnumerable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources)
        {
            if (sources == null)
                throw new ArgumentNullException(nameof(sources));

            return ConcatCore(sources);
        }

View on GitHub (pinned to 94b5d5ab91)