{"record":{"id":"112883aad31052e3","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-sources-112883","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'sources')","messagePattern":"Value cannot be null\\. \\(Parameter 'sources'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Case.cs","lineNumber":24,"sourceCode":"\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Returns a sequence from a dictionary based on the result of evaluating a selector function.\n        /// </summary>\n        /// <typeparam name=\"TValue\">Type of the selector value.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"selector\">Selector function used to pick a sequence from the given sources.</param>\n        /// <param name=\"sources\">Dictionary mapping selector values onto resulting sequences.</param>\n        /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>\n        public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)\n        {\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n            if (sources == null)\n                throw new ArgumentNullException(nameof(sources));\n\n            return Case(selector, sources, []);\n        }\n\n        /// <summary>\n        /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a\n        /// default sequence.\n        /// </summary>\n        /// <typeparam name=\"TValue\">Type of the selector value.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"selector\">Selector function used to pick a sequence from the given sources.</param>\n        /// <param name=\"sources\">Dictionary mapping selector values onto resulting sequences.</param>\n        /// <param name=\"defaultSource\">\n        /// Default sequence to return in case there's no corresponding source for the computed\n        /// selector value.\n        /// </param>\n        /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>\n        public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Case.cs#L6-L42","documentation":"Case requires a dictionary mapping selector values to sequences. When the sources dictionary is null, the operator throws ArgumentNullException with parameter name 'sources' during the eager argument check, before enumeration.","triggerScenarios":"Calling Case<TValue,TResult>(selector, null) or the overload Case(selector, null, defaultSource) with a null sources dictionary.","commonSituations":"Dictionary built conditionally or loaded from configuration that failed and returned null; variable shadowing leaving the map uninitialized.","solutions":["Pass a non-null IDictionary<TValue, IEnumerable<TResult>> (use an empty dictionary if no cases)","Verify the code path that constructs the dictionary","If an empty mapping is intentional, pass new Dictionary<...>() rather than null"],"exampleFix":"// before\nIDictionary<int, IEnumerable<int>> map = TryLoadMap(); // may be null\nvar seq = Case(getKey, map);\n// after\nvar seq = Case(getKey, TryLoadMap() ?? new Dictionary<int, IEnumerable<int>>());","handlingStrategy":"validation","validationCode":"if (sources == null) sources = new Dictionary<TValue, IEnumerable<TResult>>();","typeGuard":"bool IsValidCaseCall<TValue,TResult>(IDictionary<TValue, IEnumerable<TResult>> sources) => sources != null;","tryCatchPattern":"try { seq = Case(selector, sources); } catch (ArgumentNullException ex) when (ex.ParamName == \"sources\") { seq = Enumerable.Empty<TResult>(); }","preventionTips":["Initialize dictionaries before passing to Case","Use an empty dictionary rather than null to mean 'no cases'","Validate config-loaded mappings for null before use"],"tags":["ix","null-argument","linq","argument-validation"],"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"}