{"record":{"id":"e28107655b44799d","repo":"dotnet/reactive","slug":"nameof-observer-observable-conversions","errorCode":null,"errorMessage":"nameof(observer)","messagePattern":"nameof\\(observer\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Conversions.cs","lineNumber":31,"sourceCode":"\n        /// <summary>\n        /// Subscribes an observer to an enumerable sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Enumerable sequence to subscribe to.</param>\n        /// <param name=\"observer\">Observer that will receive notifications from the enumerable sequence.</param>\n        /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"observer\"/> is null.</exception>\n        public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (observer == null)\n            {\n                throw new ArgumentNullException(nameof(observer));\n            }\n\n            return s_impl.Subscribe(source, observer);\n        }\n\n        /// <summary>\n        /// Subscribes an observer to an enumerable sequence, using the specified scheduler to run the enumeration loop.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Enumerable sequence to subscribe to.</param>\n        /// <param name=\"observer\">Observer that will receive notifications from the enumerable sequence.</param>\n        /// <param name=\"scheduler\">Scheduler to perform the enumeration on.</param>\n        /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"observer\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer, IScheduler scheduler)\n        {\n            if (source == null)\n            {","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Conversions.cs#L13-L49","documentation":"IEnumerable.Subscribe(observer) throws ArgumentNullException when the IObserver<TSource> is null. The observer is the sink for OnNext/OnCompleted from the enumerable; without it the operation is meaningless, so Rx rejects null before subscribing.","triggerScenarios":"Calling source.Subscribe(observer) with observer == null — e.g. a cached observer that failed to initialize, or a delegate-built observer returned null from a factory.","commonSituations":"Seen when an observer instance is resolved from DI or a registry and comes back null, or when code passes a field that is only assigned in an async initialization path that has not completed.","solutions":["Pass a valid IObserver<TSource> implementation (or use the Action-based Subscribe overloads).","Check the observer-producing factory/registry for the code path returning null.","Create the observer before starting the subscription rather than in a later initialization step."],"exampleFix":"// before\nIObserver<int> obs = _observerHolder.Current; // null until init completes\nsource.Subscribe(obs); // throws\n// after\nif (_observerHolder.Current is IObserver<int> obs)\n    source.Subscribe(obs);\nelse\n    source.Subscribe(x => Console.WriteLine(x));","handlingStrategy":"validation","validationCode":"if (observer is null) observer = new AnonymousObserver<T>(x => Console.WriteLine(x));\nvar d = source.Subscribe(observer);","typeGuard":"static bool HasObserver<T>(IObserver<T>? o) => o is not null;","tryCatchPattern":"try { var d = source.Subscribe(observer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { log.Error(\"Null observer passed to Subscribe\"); }","preventionTips":["Prefer the Action-based Subscribe overloads to avoid passing observer objects.","Verify DI/registry observer resolution before subscribing.","Initialize observers synchronously before starting subscriptions."],"tags":["rx","argumentnull","observer","null-check"],"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"}