{"record":{"id":"1e1350ef7ad5a67a","repo":"dotnet/reactive","slug":"source-singleordefault","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/SingleOrDefault.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> SingleOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source) => SingleOrDefault(source);\n        public static IAsyncObservable<TSource> SingleOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => SingleOrDefault(source, predicate);\n        public static IAsyncObservable<TSource> SingleOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => SingleOrDefault(source, predicate);\n\n        public static IAsyncObservable<TSource> SingleOrDefault<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.SingleOrDefault(observer)));\n        }\n\n        public static IAsyncObservable<TSource> SingleOrDefault<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 CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.SingleOrDefault(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> SingleOrDefault<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/SingleOrDefault.cs#L1-L36","documentation":"Thrown by SingleOrDefault(source) when the source IAsyncObservable is null. SingleOrDefault differs from Single only in tolerating empty sequences, not null arguments — null source is still an immediate ArgumentNullException. The library's convention is eager validation in every public operator.","triggerScenarios":"Calling SingleOrDefaultAsync(source) or SingleOrDefault(source) with a null source observable.","commonSituations":"Observables obtained from caches, dictionaries, or DI that can be null; chained queries where an upstream factory returned null.","solutions":["Verify the source is non-null before calling SingleOrDefault","Substitute AsyncObservable.Empty<TSource>() when a null source should mean 'no elements'","Find the upstream producer returning null and fix it there"],"exampleFix":"// before\nvar single = await source.SingleOrDefaultAsync();\n// after\nvar single = await (source ?? AsyncObservable.Empty<int>()).SingleOrDefaultAsync();","handlingStrategy":"validation","validationCode":"if (source is null) source = AsyncObservable.Empty<T>();","typeGuard":"static bool IsQueryable<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var v = await source.SingleOrDefaultAsync(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* treat as empty sequence */ }","preventionTips":["Treat null and empty as distinct: normalize null to Empty early","Ensure repository/cache lookups return Empty observables, not null","Enable nullable reference types to catch unassigned observable fields at compile time"],"tags":["csharp","null-check","argument-validation","asyncrx"],"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"}