{"record":{"id":"d81c8415b965f5f5","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-source-scan","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(source));","messagePattern":"throw new ArgumentNullException\\(nameof\\(source\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Scan.cs","lineNumber":14,"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,","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Scan.cs#L1-L32","documentation":"The Scan<TSource>(source, func) operator throws ArgumentNullException when the source async observable is null. Scan aggregates emissions with an accumulator, so it validates the source synchronously at call time (the func is validated immediately after) before building the operator.","triggerScenarios":"Calling source.Scan(accumulator) with a null source — an uninitialized stream, a factory that returned null, or applying Scan to the result of a lookup/conditional that missed.","commonSituations":"Running totals over a stream whose producer failed silently; pipeline builders where an earlier operator conditionally returned null; tests where the subject was never assigned.","solutions":["Create the source observable before calling Scan","Coalesce to an empty stream: source ?? AsyncObservable.Empty<TSource>()","Guard and throw a descriptive exception in your composition helper","Fix the upstream method that returns null for the source"],"exampleFix":"// before\nvar total = events.Scan((acc, x) => acc + x); // events null\n// after\nvar src = events ?? AsyncObservable.Empty<int>();\nvar total = src.Scan((acc, x) => acc + x);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nvar scanned = source.Scan((acc, x) => acc + x);","typeGuard":"static bool HasSource<TSource>(IAsyncObservable<TSource>? s) => s is not null;","tryCatchPattern":"try\n{\n    var scanned = source.Scan(accumulate);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    scanned = AsyncObservable.Empty<TSource>();\n}","preventionTips":["Never return null from stream-producing methods; return Empty","Compose operators immediately after stream creation in one place","Enable nullable reference types to catch null sources at compile time","Validate both source and delegate in custom pipeline builders"],"tags":["csharp","null-argument","async-reactive","argument-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"}