{"record":{"id":"75f38d5318be2ee0","repo":"dotnet/reactive","slug":"source-observable-extensions","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs","lineNumber":31,"sourceCode":"    /// Provides a set of static methods for subscribing delegates to observables.\n    /// </summary>\n    public static class ObservableExtensions\n    {\n        #region Subscribe delegate-based overloads\n\n        /// <summary>\n        /// Subscribes to the observable sequence without specifying any handlers.\n        /// This method can be used to evaluate the observable sequence for its side-effects only.\n        /// </summary>\n        /// <typeparam name=\"T\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Observable sequence to subscribe to.</param>\n        /// <returns><see cref=\"IDisposable\"/> object used to unsubscribe from the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is <c>null</c>.</exception>\n        public static IDisposable Subscribe<T>(this IObservable<T> source)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            //\n            // [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally.\n            //\n            return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(Stubs<T>.Ignore, Stubs.Throw, Stubs.Nop));\n        }\n\n        /// <summary>\n        /// Subscribes an element handler to an observable sequence.\n        /// </summary>\n        /// <typeparam name=\"T\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Observable sequence to subscribe to.</param>\n        /// <param name=\"onNext\">Action to invoke for each element in the observable sequence.</param>\n        /// <returns><see cref=\"IDisposable\"/> object used to unsubscribe from the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"onNext\"/> is <c>null</c>.</exception>\n        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext)\n        {","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs#L13-L49","documentation":"The Subscribe<T>(this IObservable<T>) extension throws ArgumentNullException when the source sequence is null. Rx validates the receiver before constructing the AnonymousObserver that ignores values, rethrows errors, and no-ops on completion. Thrown synchronously at subscription time, before any observer callbacks.","triggerScenarios":"Calling source.Subscribe() where the IObservable<T> variable is null — e.g. a factory/property returned null, a dictionary lookup missed, or a chain like GetObservable()?.Subscribe() resolved to null via !.","commonSituations":"DI/container returning null for a registered-or-not service; optional observables that are null when a feature is disabled; test doubles not stubbed.","solutions":["Ensure the source is initialized before subscribing; fix whatever returns null.","Use source?.Subscribe() with null-conditional access if subscribing a null source should be a no-op.","Use Observable.Empty<T>() as a fallback instead of null to keep the chain type-safe."],"exampleFix":"// before\nsource.Subscribe(); // source is null\n// after\n(source ?? Observable.Empty<int>()).Subscribe();","handlingStrategy":"validation","validationCode":"if (source == null) return System.Reactive.Disposables.Disposable.Empty;\nsource.Subscribe();","typeGuard":"bool IsSubscribable<T>(IObservable<T> source) => source != null;","tryCatchPattern":"try { source.Subscribe(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* log: observable source was null */ }","preventionTips":["Return Observable.Empty<T>() instead of null from factory methods that produce sequences.","Use the null-conditional operator (source?.Subscribe()) when null sources are expected and ignorable.","Check DI registrations so observable-producing services never resolve to null."],"tags":["argument-null","subscribe","reactive-extensions"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}