dotnet/reactive · error · ArgumentNullException

condition

Error message

condition

What it means

DoWhile evaluates its condition delegate after each repetition of the source; a null condition would make repetition undecidable. The wrapper throws ArgumentNullException for the condition parameter before any subscription occurs.

Solutions

  1. Supply a concrete Func<bool>, e.g. () => shouldContinue, even if it always returns false.
  2. Null-check or default-initialize the condition delegate before the call.
  3. If no repetition is desired, drop DoWhile entirely and subscribe to the source directly.

Example fix

// before
Func<bool> cond = null;
var result = source.DoWhile(cond);
// after
Func<bool> cond = () => shouldRepeat;
var result = source.DoWhile(cond);
Defensive patterns

Strategy: validation

Validate before calling

condition ??= () => false;

Type guard

bool IsValidCondition(Func<bool> c) => c is not null;

Try / catch

try { var result = source.DoWhile(condition); }
catch (ArgumentNullException ex) when (ex.ParamName == "condition") { /* supply default predicate */ }

Prevention

When it happens

Trigger: Calling Observable.DoWhile(source, null) or passing a Func<bool> variable/field that was never assigned.

Common situations: Storing the loop predicate in a nullable delegate field, conditional predicate setup that skipped assignment, or refactoring that renamed the condition method leaving null behind.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Imperative.cs:228

        /// <summary>
        /// Repeats the given <paramref name="source"/> as long as the specified <paramref name="condition"/> holds, where the <paramref name="condition"/> is evaluated after each repeated <paramref name="source"/> completed.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source to repeat as long as the <paramref name="condition"/> function evaluates to true.</param>
        /// <param name="condition">Condition that will be evaluated upon the completion of an iteration through the <paramref name="source"/>, to determine whether repetition of the source is required.</param>
        /// <returns>The observable sequence obtained by concatenating the <paramref name="source"/> sequence as long as the <paramref name="condition"/> holds.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="condition"/> is null.</exception>
        public static IObservable<TSource> DoWhile<TSource>(this IObservable<TSource> source, Func<bool> condition)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return s_impl.DoWhile(source, condition);
        }

        #endregion

        #region + For +

        /// <summary>
        /// Concatenates the observable sequences obtained by running the <paramref name="resultSelector"/> for each element in the given enumerable <paramref name="source"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the enumerable source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the observable result sequence.</typeparam>
        /// <param name="source">Enumerable source for which each element will be mapped onto an observable source that will be concatenated in the result sequence.</param>
        /// <param name="resultSelector">Function to select an observable source for each element in the <paramref name="source"/>.</param>
        /// <returns>The observable sequence obtained by concatenating the sources returned by <paramref name="resultSelector"/> for each element in the <paramref name="source"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="resultSelector"/> is null.</exception>

View on GitHub (pinned to 94b5d5ab91)