{"record":{"id":"0c3e810269cdb544","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-defaultsource-case","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'defaultSource')","messagePattern":"Value cannot be null\\. \\(Parameter 'defaultSource'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Case.cs","lineNumber":49,"sourceCode":"        /// 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)\n        {\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n            if (sources == null)\n                throw new ArgumentNullException(nameof(sources));\n            if (defaultSource == null)\n                throw new ArgumentNullException(nameof(defaultSource));\n\n            return Defer(() =>\n            {\n                if (!sources.TryGetValue(selector(), out var result))\n                {\n                    result = defaultSource;\n                }\n\n                return result;\n            });\n        }\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Case.cs#L31-L63","documentation":"The Case operator throws ArgumentNullException with parameter name 'defaultSource' when the fallback sequence used when the selector's value has no dictionary entry is null. Ix.NET validates all arguments eagerly so the failure happens at the call site. Use an empty sequence, not null, to express 'no fallback elements'.","triggerScenarios":"Calling Case<TValue,TResult>(selector, sources, defaultSource) with null as the third argument (the fallback sequence).","commonSituations":"A factory or method call returning null as the default source; assuming null means 'nothing' instead of Enumerable.Empty<T>(); legacy code using null sentinels for empty sequences.","solutions":["Pass Enumerable.Empty<TResult>() instead of null for an empty fallback.","Check the expression producing defaultSource for an early return or failed lookup yielding null.","If null is possible upstream, coalesce: defaultSource ?? Enumerable.Empty<TResult>().","Enable nullable reference type annotations to catch this at compile time."],"exampleFix":"// before\nvar seq = Case(() => key, sources, null);\n// after\nvar seq = Case(() => key, sources, Enumerable.Empty<int>());","handlingStrategy":"validation","validationCode":"if (defaultSource is null)\n    throw new ArgumentNullException(nameof(defaultSource));\n// or normalize:\ndefaultSource ??= Enumerable.Empty<TResult>();","typeGuard":"static bool HasDefault<TResult>(IEnumerable<TResult> def) => def != null;","tryCatchPattern":"try\n{\n    var seq = Case(selector, sources, defaultSource);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"defaultSource\")\n{\n    var seq = Case(selector, sources, Enumerable.Empty<TResult>());\n}","preventionTips":["Express 'no fallback' with Enumerable.Empty<T>(), never null.","Coalesce nullable sequences with ?? Enumerable.Empty<T>() at call sites.","Enable nullable reference types to catch null sequence arguments at compile time.","Avoid factory methods for sequences that can return null; return empty sequences instead."],"tags":["csharp","null-argument","linq","ix-net","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"}