{"record":{"id":"1f8e700331bcc902","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-predicate-count","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'predicate')","messagePattern":"Value cannot be null\\. \\(Parameter 'predicate'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Count.cs","lineNumber":24,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<int> Count<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, int>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<TSource>(observer)));\n        }\n\n        public static IAsyncObservable<int> Count<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<int>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));\n        }\n\n        public static IAsyncObservable<int> Count<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<int>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Count.cs#L6-L42","documentation":"Count(source, Func<TSource,bool>) throws ArgumentNullException because the synchronous predicate delegate is null. The predicate is applied per element before counting; without it the operator cannot decide which elements to include. It is validated eagerly at the Count() call site.","triggerScenarios":"Passing null as the predicate: e.g. a delegate variable that was never assigned, a conditional lambda expression that evaluated to null, or a method group on a null object reference.","commonSituations":"Storing predicates in fields/config for dynamic filtering where the filter was never set; DI-injected filter functions defaulting to null; overloading confusion where the async predicate overload was intended but null was passed.","solutions":["Pass a concrete predicate, e.g. Count(x => x > 0).","If no filtering is needed, call the parameterless Count() overload instead of passing null.","Default the predicate variable to a constant-true lambda: _ => true."],"exampleFix":"// before\nFunc<int, bool> pred = ConfigureFilter(); // may return null\nvar n = await source.Count(pred);\n// after\nvar n = await source.Count(ConfigureFilter() ?? (_ => true));","handlingStrategy":"validation","validationCode":"if (predicate is null) throw new ArgumentNullException(nameof(predicate));","typeGuard":"static bool HasPredicate<T>(Func<T,bool>? p) => p is not null;","tryCatchPattern":"try { var n = await source.Count(pred); } catch (ArgumentNullException ex) when (ex.ParamName == \"predicate\") { pred = _ => true; }","preventionTips":["Default filter delegates to _ => true rather than null.","Call the parameterless Count() when no filtering is needed.","Keep predicates in non-nullable fields with eager initialization."],"tags":["argument-null","predicate","async-rx"],"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"}