{"record":{"id":"5d171d84e3b225e4","repo":"dotnet/reactive","slug":"nameof-observablefactory","errorCode":null,"errorMessage":"nameof(observableFactory)","messagePattern":"nameof\\(observableFactory\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":192,"sourceCode":"            return s_impl.Create(subscribeAsync);\n        }\n\n        #endregion\n\n        #region + Defer +\n\n        /// <summary>\n        /// Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>\n        /// <param name=\"observableFactory\">Observable factory function to invoke for each observer that subscribes to the resulting sequence.</param>\n        /// <returns>An observable sequence whose observers trigger an invocation of the given observable factory function.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"observableFactory\"/> is null.</exception>\n        public static IObservable<TResult> Defer<TResult>(Func<IObservable<TResult>> observableFactory)\n        {\n            if (observableFactory == null)\n            {\n                throw new ArgumentNullException(nameof(observableFactory));\n            }\n\n            return s_impl.Defer(observableFactory);\n        }\n\n        #endregion\n\n        #region + DeferAsync +\n\n        /// <summary>\n        /// Returns an observable sequence that starts the specified asynchronous factory function whenever a new observer subscribes.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>\n        /// <param name=\"observableFactoryAsync\">Asynchronous factory function to start for each observer that subscribes to the resulting sequence.</param>\n        /// <returns>An observable sequence whose observers trigger the given asynchronous observable factory function to be started.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"observableFactoryAsync\"/> is null.</exception>\n        /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>\n        public static IObservable<TResult> Defer<TResult>(Func<Task<IObservable<TResult>>> observableFactoryAsync)","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L174-L210","documentation":"Observable.Defer<TResult> takes a factory Func<IObservable<TResult>> invoked per subscription. Rx throws ArgumentNullException immediately when the factory is null because a null factory would make every subscription fail anyway; failing at composition gives a better stack trace.","triggerScenarios":"Calling Observable.Defer<TResult>(observableFactory) with a null Func<IObservable<TResult>> argument.","commonSituations":"A nullable Func field assigned later, a configuration-driven factory lookup returning null, or caching logic that stores null when the real factory failed to build.","solutions":["Pass a non-null factory lambda, e.g. () => Observable.Return(value).","Null-check the factory before calling Defer.","Fix the factory provider/registry so it never yields null."],"exampleFix":"// before\nvar obs = Observable.Defer(factory); // factory is null\n// after\nvar obs = Observable.Defer(factory ?? (() => Observable.Empty<int>()));","handlingStrategy":"validation","validationCode":"if (observableFactory is null) throw new ArgumentNullException(nameof(observableFactory));\nvar obs = Observable.Defer(observableFactory);","typeGuard":"bool IsValid<T>(Func<IObservable<T>> f) => f is not null;","tryCatchPattern":"try { var obs = Observable.Defer(observableFactory); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observableFactory\") { /* fall back to Observable.Empty */ }","preventionTips":["Prefer inline lambdas: () => CreateSequence()","Null-check configuration-driven factories before composing","Cache delegates with non-null guarantees, never cache null on failure"],"tags":["argumentnull","reactive-extensions","defer"],"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"}