{"record":{"id":"2a62aada0f684b99","repo":"dotnet/reactive","slug":"argumentnullexception-nameof-observer-tolist","errorCode":null,"errorMessage":"ArgumentNullException(nameof(observer))","messagePattern":"ArgumentNullException\\(nameof\\(observer\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToList.cs","lineNumber":25,"sourceCode":"namespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<IList<TSource>> ToList<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, IList<TSource>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToList(observer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ToList<TSource>(IAsyncObserver<IList<TSource>> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Aggregate<TSource, List<TSource>, IList<TSource>>(observer, new List<TSource>(), (xs, x) => { xs.Add(x); return xs; }, xs => xs);\n        }\n    }\n}\n","sourceCodeStart":7,"sourceCodeEnd":31,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToList.cs#L7-L31","documentation":"AsyncObserver.ToList(observer) converts a downstream observer into one that buffers elements into a List<TSource> and emits it on completion. The observer argument must be non-null; otherwise ArgumentNullException(nameof(observer)) is thrown synchronously. This is part of the internal observer-composition API.","triggerScenarios":"Calling AsyncObserver.ToList(null) — typically inside a custom Create-based operator where the observer was not forwarded from the subscribe callback.","commonSituations":"Hand-written operator plumbing where the observer parameter of the Create lambda was shadowed or dropped; test harnesses that forgot to instantiate a stub observer.","solutions":["Forward the actual observer from your Create<TSource,TResult> callback into AsyncObserver.ToList.","Add an argument guard in your own code before composing observer chains.","Rename shadowed variables so the lambda's observer parameter is the one passed on."],"exampleFix":"// before\nCreate<int, IList<int>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToList(obs))) // wrong var\n// after\nCreate<int, IList<int>>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToList(observer)))","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentNullException(nameof(observer));","typeGuard":"bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObserver.ToList(observer); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* fix observer wiring */ }","preventionTips":["Pass the Create callback's observer parameter directly, never a captured variable","Avoid lambda parameter shadowing in operator pipelines","Unit-test custom operators with an immediate subscription"],"tags":["null-argument","asyncrx","observer"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}