{"record":{"id":"cadfc786e0b66fba","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-func","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'func')","messagePattern":"Value cannot be null\\. \\(Parameter 'func'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs","lineNumber":16,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, TSource> func)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                func,\n                static (source, func, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, func)));\n        }\n\n        public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> func)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                func,\n                static (source, func, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, func)));","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs#L1-L34","documentation":"System.ArgumentNullException thrown by the sync Aggregate overload of IAsyncObservable<T> when the accumulator delegate 'func' is null. The operator eagerly validates all arguments before creating the subscription pipeline, since a null accumulator would only fail mid-stream otherwise. This fail-fast style is used across all AsyncRx.NET operators.","triggerScenarios":"Calling source.Aggregate(null) — i.e. invoking the two-generic-parameter Aggregate extension (Aggregate.cs:16 guard) with a null Func<TSource,TSource,TSource>.","commonSituations":"Passing a method group that resolves to null (e.g. a nullable static field holding a comparer/accumulator), conditional delegates assigned from configuration that was never initialized, or refactoring that removed the lambda but left the call site.","solutions":["Pass a non-null accumulator lambda, e.g. source.Aggregate((acc, x) => acc + x).","If the delegate comes from a variable, check it for null before calling Aggregate and substitute a default.","Ensure any factory/method-group expression used as 'func' actually returns a delegate instance at runtime."],"exampleFix":"// before\nFunc<int, int, int> acc = GetAccumulator(); // may return null\nvar result = source.Aggregate(acc);\n\n// after\nvar acc = GetAccumulator() ?? ((a, x) => a + x);\nvar result = source.Aggregate(acc);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nif (func is null) throw new ArgumentNullException(nameof(func));\nvar result = source.Aggregate(func);","typeGuard":"static bool IsValidAggregateArgs<TSource>(IAsyncObservable<TSource>? source, Func<TSource, TSource, TSource>? func)\n    => source is not null && func is not null;","tryCatchPattern":"try { var result = source.Aggregate(func); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"func\") { /* supply default accumulator */ }","preventionTips":["Never pass nullable delegate fields directly into operator calls; coalesce with a default.","Prefer inline lambdas over method groups that can resolve to null.","Enable nullable reference types (NRT) so the compiler flags nullable delegates."],"tags":["null-argument","argument-validation","linq","reactive"],"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"}