{"record":{"id":"13e91736fa9fb143","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-enumerablefactory","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'enumerableFactory')","messagePattern":"Value cannot be null\\. \\(Parameter 'enumerableFactory'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Defer.cs","lineNumber":20,"sourceCode":"// 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.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Creates an enumerable sequence based on an enumerable factory function.\n        /// </summary>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"enumerableFactory\">Enumerable factory function.</param>\n        /// <returns>Sequence that will invoke the enumerable factory upon a call to GetEnumerator.</returns>\n        public static IEnumerable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enumerableFactory)\n        {\n            if (enumerableFactory == null)\n                throw new ArgumentNullException(nameof(enumerableFactory));\n\n            return DeferCore(enumerableFactory);\n        }\n\n        private static IEnumerable<TSource> DeferCore<TSource>(Func<IEnumerable<TSource>> enumerableFactory)\n        {\n            foreach (var item in enumerableFactory())\n            {\n                yield return item;\n            }\n        }\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Defer.cs#L2-L34","documentation":"Defer creates a sequence whose factory is invoked on each GetEnumerator call. The library throws ArgumentNullException when enumerableFactory is null, guarding the deferred pipeline at construction time so the failure is attributable to the caller, not a later MoveNext.","triggerScenarios":"Calling EnumerableEx.Defer<TResult>(null) directly, or Case/If operators receiving a null sequence-factory (or a factory map with null entries) that they forward to Defer.","commonSituations":"A dictionary of branch factories where the requested key is missing and TryGetValue leaves the delegate null, a conditional expression with a null branch, or a lazily-initialized factory never assigned.","solutions":["Ensure the Func<IEnumerable<TResult>> passed to Defer is non-null","If using Case/If, verify every branch supplies a valid factory and that the selector always hits a defined branch","Throw a descriptive exception or substitute Enumerable.Empty<TResult>() when the factory is legitimately absent"],"exampleFix":"// before\nfactories.TryGetValue(key, out Func<IEnumerable<int>> factory);\nvar seq = EnumerableEx.Defer(factory); // factory may be null\n// after\nif (factory == null)\n    throw new KeyNotFoundException($\"No sequence factory for '{key}'.\");\nvar seq = EnumerableEx.Defer(factory);","handlingStrategy":"validation","validationCode":"if (enumerableFactory == null)\n    throw new ArgumentNullException(nameof(enumerableFactory));\nvar seq = EnumerableEx.Defer(enumerableFactory);","typeGuard":"static bool IsValidFactory<TResult>(Func<IEnumerable<TResult>> factory) => factory is not null;","tryCatchPattern":"try\n{\n    var seq = EnumerableEx.Defer(factory);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"enumerableFactory\")\n{\n    // fall back to an empty sequence or throw a domain-specific error\n}","preventionTips":["When branching on a key, throw on unknown keys instead of leaving the factory null","Use exhaustive switch expressions so every branch yields a factory","Coalesce with () => Enumerable.Empty<TResult>() when absence is legitimate"],"tags":["null-argument","csharp","ix-net","lazy-evaluation"],"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"}