dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'predicate')
Error message
Value cannot be null. (Parameter 'predicate')
What it means
The predicate overloads of LongCount require a non-null filter delegate; the sync overload at LongCount.cs:24 throws ArgumentNullException when Func<TSource, bool> predicate is null. Without a predicate the operator cannot decide which elements to count, so it fails fast.
Solutions
- Pass a concrete predicate, e.g. x => x.IsActive.
- If no filtering is desired, call the parameterless source.LongCount() overload instead of passing null.
- Default null delegates to a pass-through filter: predicate ?? (_ => true).
Example fix
// before source.LongCount(predicate); // predicate is null // after source.LongCount(predicate ?? (_ => true));
Defensive patterns
Strategy: validation
Validate before calling
if (predicate is null) predicate = static _ => true; var count = source.LongCount(predicate);
Type guard
static bool HasPredicate<T>(Func<T, bool>? p) => p is not null;
Try / catch
try { var count = source.LongCount(predicate); }
catch (ArgumentNullException ex) when (ex.ParamName == "predicate") { var count = source.LongCount(); } Prevention
- Use the parameterless LongCount() overload when no filter is needed instead of passing null.
- Initialize filter delegate fields at declaration.
- Avoid ternaries that can yield null delegates; use ?? (_ => true).
When it happens
Trigger: Calling source.LongCount((Func<T, bool>)null) — the sync-predicate overload at LongCount.cs:24 with a null predicate delegate.
Common situations: Storing a filter in a field/variable that was never initialized; passing the result of a conditional expression like cond ? myFilter : null; reflection-driven code resolving a delegate that came back null.
Related errors
- conversion
- addHandler
- Value cannot be null. (Parameter 'condition')
- Value cannot be null. (Parameter 'observer')
- Value cannot be null. (Parameter 'source')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/6aaed6d82fb47dd3.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/LongCount.cs:24
namespace System.Reactive.Linq
{
public partial class AsyncObservable
{
public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return Create<TSource, long>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount<TSource>(observer)));
}
public static IAsyncObservable<long> LongCount<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 CreateAsyncObservable<long>.From(
source,
predicate,
static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));
}
public static IAsyncObservable<long> LongCount<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 CreateAsyncObservable<long>.From(
source,
predicate,
static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));View on GitHub (pinned to 94b5d5ab91)