dotnet/reactive · error · ArgumentNullException

nameof(condition)

Error message

nameof(condition)

What it means

The public AsyncObservable.Generate overload requires a condition predicate and throws ArgumentNullException with the parameter name 'condition' when it is null. Generate drives a for-loop-like async sequence, so a null condition cannot be substituted by a library default.

Solutions

  1. Pass a non-null predicate, e.g. x => x < 10, as the condition argument
  2. If the condition is computed, guard with ?? throw before calling Generate
  3. Ensure any factory/DI source producing the predicate actually returns a delegate

Example fix

// before
AsyncObservable.Generate(0, null, x => x + 1, x => x);
// after
AsyncObservable.Generate(0, x => x < 10, x => x + 1, x => x);
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null)
    throw new ArgumentNullException(nameof(condition), "Generate requires a condition predicate");
var obs = AsyncObservable.Generate(state, condition, iterate, resultSelector);

Type guard

static bool IsValidGenerateArgs<TState, TResult>(Func<TState, bool>? condition, Func<TState, TState>? iterate, Func<TState, TResult>? resultSelector)
    => condition != null && iterate != null && resultSelector != null;

Try / catch

try
{
    var obs = AsyncObservable.Generate(state, condition, iterate, selector);
}
catch (ArgumentNullException ex)
{
    logger.LogError(ex, "Generate received a null delegate: {Param}", ex.ParamName);
}

Prevention

When it happens

Trigger: Calling AsyncObservable.Generate(initialState, null, iterate, resultSelector) — passing null for the Func<TState,bool> condition argument, often because the predicate is built dynamically or returned null from a factory.

Common situations: Condition lambdas supplied via optional config that defaults to null; refactoring that lost the predicate; dependency-injected delegates not registered and resolving to null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs:15

// 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.Concurrency;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (iterate == null)
                throw new ArgumentNullException(nameof(iterate));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));

            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));
        }

        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (iterate == null)
                throw new ArgumentNullException(nameof(iterate));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

View on GitHub (pinned to 94b5d5ab91)