dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(sources))

Error message

ArgumentNullException(nameof(sources))

What it means

The IEnumerable overload CombineLatest<TSource,TResult>(sources, resultSelector) throws ArgumentNullException when the sources sequence is null (Observable.Multiple.cs:294). Validation runs eagerly at call time, before the resultSelector check. This fail-fast guard prevents the error from surfacing later inside the subscription pipeline.

Solutions

  1. Initialize sources to an empty enumerable when there are no streams: Enumerable.Empty<IObservable<TSource>>().
  2. Coalesce at the call site: (sources ?? Enumerable.Empty<IObservable<TSource>>()).CombineLatest(selector).
  3. Fix the producer method so it returns an empty collection rather than null.

Example fix

// before
var streams = registry.GetStreams(name); // can return null
var combined = streams.CombineLatest(xs => xs.Sum());
// after
var streams = registry.GetStreams(name) ?? Enumerable.Empty<IObservable<int>>();
var combined = streams.CombineLatest(xs => xs.Sum());
Defensive patterns

Strategy: validation

Validate before calling

if (sources == null) throw new InvalidOperationException("sources required"); // or: sources ??= Enumerable.Empty<IObservable<TSource>>();

Type guard

bool HasSources(IEnumerable<IObservable<TSource>>? sources) => sources is not null;

Try / catch

try { var result = sources.CombineLatest(selector); } catch (ArgumentNullException ex) when (ex.ParamName == "sources") { /* fall back to empty sequence */ }

Prevention

When it happens

Trigger: Calling sources.CombineLatest(selector) with a null IEnumerable<IObservable<TSource>>, e.g. Observable.CombineLatest(GetStreams(), selector) where GetStreams() returns null.

Common situations: Streams collected from a dictionary lookup that returned null; a list built conditionally and left uninitialized; framework code passing a nullable collection straight through.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Multiple.cs:294

            return s_impl.CombineLatest(first, second, resultSelector);
        }

        /// <summary>
        /// Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
        /// <param name="sources">Observable sources.</param>
        /// <param name="resultSelector">Function to invoke whenever any of the sources produces an element. For efficiency, the input list is reused after the selector returns. Either aggregate or copy the values during the function call.</param>
        /// <returns>An observable sequence containing the result of combining elements of the sources using the specified result selector function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="sources"/> or <paramref name="resultSelector"/> is null.</exception>
        /// <remarks>If a non-empty source completes, its very last value will be used for creating subsequent combinations until all sources terminate.</remarks>
        public static IObservable<TResult> CombineLatest<TSource, TResult>(this IEnumerable<IObservable<TSource>> sources, Func<IList<TSource>, TResult> resultSelector)
        {
            if (sources == null)
            {
                throw new ArgumentNullException(nameof(sources));
            }

            if (resultSelector == null)
            {
                throw new ArgumentNullException(nameof(resultSelector));
            }

            return s_impl.CombineLatest(sources, resultSelector);
        }

        /// <summary>
        /// Merges the specified observable sequences into one observable sequence by emitting a list with the latest source elements whenever any of the observable sequences produces an element.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequences, and in the lists in the result sequence.</typeparam>
        /// <param name="sources">Observable sources.</param>
        /// <returns>An observable sequence containing lists of the latest elements of the sources.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
        /// <remarks>If a non-empty source completes, its very last value will be used for creating subsequent combinations until all sources terminate.</remarks>

View on GitHub (pinned to 94b5d5ab91)