{"record":{"id":"1248c9e80ecc0c0b","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-enumerablefactory-using","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'enumerableFactory')","messagePattern":"Value cannot be null\\. \\(Parameter 'enumerableFactory'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Using.cs","lineNumber":25,"sourceCode":"namespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence usage\n        /// duration.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source element type.</typeparam>\n        /// <typeparam name=\"TResource\">Resource type.</typeparam>\n        /// <param name=\"resourceFactory\">Resource factory function.</param>\n        /// <param name=\"enumerableFactory\">Enumerable factory function, having access to the obtained resource.</param>\n        /// <returns>Sequence whose use controls the lifetime of the associated obtained resource.</returns>\n        public static IEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable\n        {\n            if (resourceFactory == null)\n                throw new ArgumentNullException(nameof(resourceFactory));\n            if (enumerableFactory == null)\n                throw new ArgumentNullException(nameof(enumerableFactory));\n\n            return UsingCore(resourceFactory, enumerableFactory);\n        }\n\n        private static IEnumerable<TSource> UsingCore<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable\n        {\n            using var res = resourceFactory();\n\n            foreach (var item in enumerableFactory(res))\n            {\n                yield return item;\n            }\n        }\n    }\n}\n","sourceCodeStart":7,"sourceCodeEnd":41,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Using.cs#L7-L41","documentation":"Using<TSource,TResource> requires an enumerableFactory that maps the acquired resource to the sequence to produce. A null enumerableFactory is rejected eagerly with ArgumentNullException naming `enumerableFactory`, before any resource is acquired or disposed.","triggerScenarios":"Calling `Using(() => OpenResource(), null)`, commonly when the body lambda is built dynamically, loaded from config, or passed through an intermediary that lost it.","commonSituations":"Pipeline builders where per-resource logic is pluggable and a plugin failed to register, or refactors that accidentally dropped the lambda argument.","solutions":["Provide the lambda: `Using(() => Open(), r => YieldFrom(r))`.","Fix the composition layer that dropped/failed to load the delegate.","Guard: `body ?? (r => Enumerable.Empty<TSource>())`.","Keep the two delegates adjacent in the call to avoid argument misalignment during refactors."],"exampleFix":"// before\nUsing(() => OpenHandle(), null)\n// after\nUsing(() => OpenHandle(), handle => ReadItems(handle))","handlingStrategy":"validation","validationCode":"if (enumerableFactory is null) throw new ArgumentNullException(nameof(enumerableFactory));","typeGuard":"static bool CanProject<T,TR>(Func<TR,IEnumerable<T>> f) where TR : IDisposable => f is not null;","tryCatchPattern":"try { var seq = Using(resFactory, enumFactory); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"enumerableFactory\") { /* supply default projection */ }","preventionTips":["Keep both Using delegates in one call site; avoid passing them through intermediate nullable variables.","Provide default projections for optional pipeline stages.","Type-check composition builders so missing lambdas fail at compile time.","Review refactors that split factory/resource wiring."],"tags":["linq","null-argument","disposable","ix-interactive"],"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"}