{"record":{"id":"6aaed6d82fb47dd3","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-predicate-longcount","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'predicate')","messagePattern":"Value cannot be null\\. \\(Parameter 'predicate'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/LongCount.cs","lineNumber":24,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, long>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount<TSource>(observer)));\n        }\n\n        public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<long>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));\n        }\n\n        public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<long>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/LongCount.cs#L6-L42","documentation":"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.","triggerScenarios":"Calling source.LongCount((Func<T, bool>)null) — the sync-predicate overload at LongCount.cs:24 with a null predicate delegate.","commonSituations":"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.","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)."],"exampleFix":"// before\nsource.LongCount(predicate); // predicate is null\n// after\nsource.LongCount(predicate ?? (_ => true));","handlingStrategy":"validation","validationCode":"if (predicate is null) predicate = static _ => true;\nvar count = source.LongCount(predicate);","typeGuard":"static bool HasPredicate<T>(Func<T, bool>? p) => p is not null;","tryCatchPattern":"try { var count = source.LongCount(predicate); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"predicate\") { var count = source.LongCount(); }","preventionTips":["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)."],"tags":["csharp","argument-null","predicate","asyncrx"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}