dotnet/reactive · error · ArgumentNullException

nameof(condition)

Error message

nameof(condition)

What it means

Thrown by DoWhile(source, condition) when the condition Func<bool> delegate is null. The condition decides after each emission whether to repeat the source, so it is required; the library rejects null eagerly. The message names condition.

Solutions

  1. Supply a non-null Func<bool> condition
  2. Verify the delegate expression evaluates to a real function (e.g. the object holding the method is not null)
  3. For an async condition, use the DoWhile(source, Func<ValueTask<bool>>) overload with a non-null delegate
  4. Default the condition to () => false if repetition is not desired

Example fix

// before
Func<bool> cond = null;
AsyncObservable.DoWhile(source, cond); // throws
// after
Func<bool> cond = () => attempts < maxAttempts;
AsyncObservable.DoWhile(source, cond);
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null) throw new InvalidOperationException("DoWhile requires a non-null condition");
var res = AsyncObservable.DoWhile(source, condition);

Type guard

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

Try / catch

try
{
    var res = AsyncObservable.DoWhile(source, condition);
}
catch (ArgumentNullException ex) when (ex.ParamName == "condition")
{
    res = AsyncObservable.DoWhile(source, () => false); // never repeats
}

Prevention

When it happens

Trigger: Calling DoWhile with a valid source but a null condition delegate — a null method group, an uninitialized Func field, or swapped arguments.

Common situations: Feature-flag-gated predicates left null; passing a Func<ValueTask<bool>> where Func<bool> was expected via a null-returning conversion helper; test scaffolding with default parameters.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/DoWhile.cs:19

// Licensed to the .NET Foundation under one or more agreements.
// 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.Reactive.Disposables;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        // REVIEW: Use a tail-recursive sink.

        public static IAsyncObservable<TSource> DoWhile<TSource>(IAsyncObservable<TSource> source, Func<bool> condition)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));

            return Create(
                source,
                condition,
                static async (source, condition, observer) =>
                {
                    var subscription = new SerialAsyncDisposable();

                    var o = default(IAsyncObserver<TSource>);

                    o = AsyncObserver.CreateUnsafe<TSource>(
                            observer.OnNextAsync,
                            observer.OnErrorAsync,
                            MoveNext
                        );

                    async Task Subscribe()
                    {

View on GitHub (pinned to 94b5d5ab91)