{"record":{"id":"2cc306d0ebbbaa6a","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-func","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(func));","messagePattern":"throw new ArgumentNullException\\(nameof\\(func\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Scan.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> Scan<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.Scan(observer, func)));\n        }\n\n        public static IAsyncObservable<TSource> Scan<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.Scan(observer, func)));","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Scan.cs#L1-L34","documentation":"The Scan(source, func) sync accumulator overload throws ArgumentNullException because the func argument was null. System.Reactive.Async operators validate all arguments eagerly at call time so misconfiguration surfaces immediately instead of failing inside the subscription pipeline. A null accumulator cannot combine elements, so the operator refuses to be constructed.","triggerScenarios":"Calling source.Scan(null) where source is a non-null IAsyncObservable<TSource> but the Func<TSource,TSource,TSource> accumulator literal is null (e.g. a null-valued variable, field, or method return).","commonSituations":"Passing a accumulator delegate stored in a nullable field that was never assigned; receiving a null callback from a factory or configuration lookup; conditional delegate wiring where a branch left func unset.","solutions":["Pass a non-null accumulator, e.g. source.Scan((acc, x) => acc + x).","Check the variable holding the delegate for null before calling Scan and provide a default.","If the accumulator may legitimately be absent, guard with source.Scan(func ?? DefaultCombine) or skip Scan entirely."],"exampleFix":"// before\nFunc<int, int, int> combine = null;\nvar scanned = source.Scan(combine); // throws ArgumentNullException\n\n// after\nFunc<int, int, int> combine = (acc, x) => acc + x;\nvar scanned = source.Scan(combine);","handlingStrategy":"validation","validationCode":"if (func is null) throw new InvalidOperationException(\"Scan accumulator must be configured before use\");\nvar scanned = source.Scan(func);","typeGuard":"static bool IsValidAccumulator<TSource>(Func<TSource, TSource, TSource> f) => f is not null;","tryCatchPattern":"try { var scanned = source.Scan(func); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"func\") { /* substitute a default accumulator or abort pipeline setup */ }","preventionTips":["Never store delegates in nullable fields without initialization.","Enable nullable reference types so unassigned delegate variables surface at compile time.","Wrap pipeline construction in a factory that validates all delegates up front."],"tags":["argument-null","linq","async-reactive","operator-validation"],"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"}