{"record":{"id":"2e6c98bb4bc761aa","repo":"dotnet/reactive","slug":"nameof-source-elementatordefault","errorCode":null,"errorMessage":"nameof(source)","messagePattern":"nameof\\(source\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs","lineNumber":12,"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\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> ElementAtOrDefault<TSource>(this IAsyncObservable<TSource> source, int index)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (index < 0)\n                throw new ArgumentOutOfRangeException(nameof(index));\n\n            return Create(\n                source,\n                index,\n                static (source, index, observer) => source.SubscribeSafeAsync(AsyncObserver.ElementAtOrDefault(observer, index)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ElementAtOrDefault<TSource>(IAsyncObserver<TSource> observer, int index)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (index < 0)\n                throw new ArgumentOutOfRangeException(nameof(index));","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs#L1-L30","documentation":"This is an ArgumentNullException thrown by AsyncObservable.ElementAtOrDefault when the source observable is null. The extension method validates its receiver up front so the failure surfaces at the call site instead of deep inside subscription plumbing. A null source is always a caller bug.","triggerScenarios":"Invoking source.ElementAtOrDefault(index) or AsyncObservable.ElementAtOrDefault(null, index) where the observable came from an uninitialized variable, a factory that returned null, or a chain that short-circuited to null.","commonSituations":"Building observables from config/conditional factories that can return null; forgetting a null result from a lookup of registered streams; refactoring where a source field was no longer initialized.","solutions":["Ensure the IAsyncObservable<TSource> is non-null before the call","Fix the producer/factory that returned null","Guard optional sources: use source?.ElementAtOrDefault(index) or an explicit null check"],"exampleFix":"// before\nvar result = source.ElementAtOrDefault(3); // source null\n// after\nvar result = source?.ElementAtOrDefault(3);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nvar result = source.ElementAtOrDefault(index);","typeGuard":"bool HasSource<TSource>(IAsyncObservable<TSource> s) => s is not null;","tryCatchPattern":"try { var result = source.ElementAtOrDefault(index); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fall back to an empty observable */ }","preventionTips":["Make source factories return AsyncObservable.Empty<TSource>() instead of null","Use nullable reference types to catch null observable variables at compile time","Prefer source?.ElementAtOrDefault(index) for genuinely optional sources"],"tags":["argument-null","async-rx","observable"],"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"}