dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(resultSelector))

Error message

ArgumentNullException(nameof(resultSelector))

What it means

CombineLatest<TSource1,TSource2,TResult> throws ArgumentNullException when the resultSelector delegate is null (Observable.Multiple.cs:274). Both observables were non-null, so the selector check is the one that fired. Rx requires a real combining function since CombineLatest cannot proceed without it.

Solutions

  1. Supply a concrete lambda or method group as the resultSelector.
  2. Fall back to a default selector: first.CombineLatest(second, (a, b) => (a, b)) if combining logic is optional.
  3. If the selector is configurable, validate it is non-null at configuration load time.

Example fix

// before
Func<int, string, string> selector = strategy?.Selector; // may be null
var combined = a.CombineLatest(b, selector);
// after
var combined = a.CombineLatest(b, strategy?.Selector ?? ((x, y) => $"{x}:{y}"));
Defensive patterns

Strategy: validation

Validate before calling

if (resultSelector == null) throw new InvalidOperationException("resultSelector required"); // or: resultSelector ??= ((a, b) => (a, b));

Type guard

bool HasSelector(Func<TSource1, TSource2, TResult>? selector) => selector is not null;

Try / catch

try { var result = first.CombineLatest(second, resultSelector); } catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* use a default combining function */ }

Prevention

When it happens

Trigger: Calling first.CombineLatest(second, null), often when the selector is computed dynamically or conditionally assigned.

Common situations: Selector stored in a field or injected dependency that was never set; configuration selecting a strategy by name that mapped to null; refactoring where a method group no longer resolves and a null was passed instead.

Related errors


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

Appendix: source

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

        /// <param name="resultSelector">Function to invoke whenever either of the sources produces an element.</param>
        /// <returns>An observable sequence containing the result of combining elements of both sources using the specified result selector function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> 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<TSource1, TSource2, TResult>(this IObservable<TSource1> first, IObservable<TSource2> second, Func<TSource1, TSource2, TResult> resultSelector)
        {
            if (first == null)
            {
                throw new ArgumentNullException(nameof(first));
            }

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

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

            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)

View on GitHub (pinned to 94b5d5ab91)