{"record":{"id":"2cb2a10732e6a807","repo":"dotnet/reactive","slug":"nameof-source-first","errorCode":null,"errorMessage":"nameof(source)","messagePattern":"nameof\\(source\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs","lineNumber":18,"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> FirstAsync<TSource>(this IAsyncObservable<TSource> source) => First(source);\n        public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => First(source, predicate);\n        public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => First(source, predicate);\n\n        public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer)));\n        }\n\n        public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return Create(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs#L1-L36","documentation":"ArgumentNullException raised by the parameterless First operator when the source IAsyncObservable is null. First subscribes to the source and forwards the first element, so it validates the source before creating the subscription. FirstAsync overloads delegate to First, so this surfaces when calling First/FirstAsync with a null sequence.","triggerScenarios":"Calling FirstAsync(source) or source.First() where source is null — e.g. a lookup returned null, a class field was never initialized, or a method chain's earlier stage returned null.","commonSituations":"Consuming observables resolved from a dictionary/registry by a missing key; repository methods that return null instead of an empty observable; test setups where the subject was not assigned.","solutions":["Ensure the observable is non-null before calling First/FirstAsync; throw a meaningful error at the producer","Use a fallback: (source ?? AsyncObservable.Empty<int>()).First() — note First on an empty sequence errors, so prefer a DefaultIfEmpty stage if emptiness is expected","Fix the upstream method to return AsyncObservable.Empty instead of null"],"exampleFix":"// before\nvar first = await source.FirstAsync(); // source is null\n// after\nif (source == null)\n    throw new InvalidOperationException(\"Source sequence was not initialized\");\nvar first = await source.FirstAsync();","handlingStrategy":"validation","validationCode":"if (source == null)\n    throw new InvalidOperationException(\"Source sequence was not initialized\");\nvar first = await source.FirstAsync();","typeGuard":"bool IsInitialized(IAsyncObservable<int>? s) => s is not null;","tryCatchPattern":"try\n{\n    var first = await source.FirstAsync();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == nameof(source))\n{\n    throw new InvalidOperationException(\"Source sequence not initialized\", ex);\n}","preventionTips":["Return empty observables instead of null from producer methods","Guard registry/dictionary lookups for the observable before subscribing","Enable nullable reference types to catch uninitialized observables at compile time"],"tags":["csharp","null-argument","argument-validation","async-rx"],"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"}