dotnet/reactive · error · ArgumentNullException

nameof(predicate)

Error message

nameof(predicate)

What it means

ArgumentNullException thrown by First(source, Func<TSource,bool> predicate) when the predicate delegate itself is null. First uses the predicate to filter elements while searching for the first match, and a null filter is invalid. The parameter name reported is 'predicate'.

Solutions

  1. Ensure a predicate is supplied; treat 'no filter' with the predicateless First() overload instead of passing null
  2. Coalesce to an always-true predicate: source.First(x => true) or source.FirstAsync(_ => true)
  3. Fix the code that constructs/assigns the predicate so it never yields null

Example fix

// before
var item = await source.FirstAsync(filter); // filter is null
// after
var item = await (filter != null ? source.FirstAsync(filter) : source.FirstAsync());
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new ArgumentNullException(nameof(source));
var item = predicate != null
    ? await source.FirstAsync(predicate)
    : await source.FirstAsync();

Type guard

bool HasFilter<T>(Func<T,bool>? p) => p is not null;

Try / catch

try
{
    var item = await source.FirstAsync(predicate);
}
catch (ArgumentNullException ex) when (ex.ParamName == nameof(predicate))
{
    var item = await source.FirstAsync(); // no filter requested
}

Prevention

When it happens

Trigger: Calling source.First(null) or FirstAsync(source, (Func<TSource,bool>)null); predicate stored in a nullable delegate field/property that was never assigned, or built dynamically (expression-to-delegate compilation) and left null.

Common situations: Configurable filtering (UI-selected filters, rule engines) where no filter yields a null delegate; overloads resolved via reflection passing a null argument.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs:28

    {
        public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source) => First(source);
        public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => First(source, predicate);
        public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => First(source, predicate);

        public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer)));
        }

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

            return Create(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer, predicate)));
        }

        public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return Create(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer, predicate)));

View on GitHub (pinned to 94b5d5ab91)