{"record":{"id":"4cc1158db471033f","repo":"dotnet/reactive","slug":"thrown-when-selector-is-null-argumentnullexception-param","errorCode":null,"errorMessage":"Thrown when selector is null (ArgumentNullException, param name: selector)","messagePattern":"Thrown when selector is null \\(ArgumentNullException, param name: selector\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Expand.cs","lineNumber":23,"sourceCode":"using System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Expands the sequence by recursively applying a selector function.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"selector\">Selector function to retrieve the next sequence to expand.</param>\n        /// <returns>Sequence with results from the recursive expansion of the source sequence.</returns>\n        public static IEnumerable<TSource> Expand<TSource>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return ExpandCore(source, selector);\n        }\n\n        private static IEnumerable<TSource> ExpandCore<TSource>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> selector)\n        {\n            var queue = new Queue<IEnumerable<TSource>>();\n            queue.Enqueue(source);\n\n            while (queue.Count > 0)\n            {\n                var src = queue.Dequeue();\n\n                foreach (var item in src)\n                {\n                    queue.Enqueue(selector(item));\n                    yield return item;\n                }","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Expand.cs#L5-L41","documentation":"Expand recursively applies a selector to grow the sequence; a null selector would terminate the recursion logic, so ArgumentNullException for parameter 'selector' is thrown. Fix: pass a non-null Func<TSource, IEnumerable<TSource>>.","triggerScenarios":"Calling Expand(source, null) — e.g. the selector delegate was resolved from configuration or a nullable member that is null.","commonSituations":"A strategy/visitor delegate not injected; conditional wiring that skips assigning the selector; using obj?.Selector which yields null.","solutions":["Pass a real Func<TSource, IEnumerable<TSource>>; use _ => Enumerable.Empty<TSource>() for a no-expansion default.","Ensure the delegate is initialized/injected before the call.","Guard the delegate acquisition point."],"exampleFix":"// before\nvar all = source.Expand(selector); // selector may be null\n// after\nvar all = source.Expand(selector ?? (_ => Enumerable.Empty<Node>()));","handlingStrategy":"validation","validationCode":"if (selector is null) throw new ArgumentNullException(nameof(selector));\nExpand(source, selector ?? (_ => Enumerable.Empty<TSource>()));","typeGuard":"bool HasSelector<TSource>(Func<TSource, IEnumerable<TSource>> f) => f is not null;","tryCatchPattern":"try { var q = source.Expand(selector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"selector\") { /* supply default selector */ }","preventionTips":["Ensure delegates are injected/assigned before query construction.","Default optional selectors to no-op expansions.","Avoid null-propagating member access on delegates.","Use constructor injection checks to reject missing delegates."],"tags":["null-argument","argument-validation","delegate"],"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"}