{"record":{"id":"d74d2cc1fec50e45","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-defaultsource","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'defaultSource')","messagePattern":"Value cannot be null\\. \\(Parameter 'defaultSource'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Case.cs","lineNumber":24,"sourceCode":"using System.Reactive.Concurrency;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IAsyncObservable<TResult>> sources) => Case(selector, sources, Empty<TResult>());\n\n        public static IAsyncObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IAsyncObservable<TResult>> sources, IAsyncScheduler scheduler) => Case(selector, sources, Empty<TResult>(scheduler));\n\n        public static IAsyncObservable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IAsyncObservable<TResult>> sources, IAsyncObservable<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 Create<TResult>(observer =>\n            {\n                var source = default(IAsyncObservable<TResult>);\n\n                try\n                {\n                    var value = selector();\n\n                    if (!sources.TryGetValue(value, out source))\n                    {\n                        source = defaultSource;\n                    }\n                }\n                catch (Exception ex)\n                {\n                    return Throw<TResult>(ex).SubscribeAsync(observer);\n                }","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Case.cs#L6-L42","documentation":"Thrown by Case<TValue, TResult>(selector, sources, defaultSource) when the defaultSource observable is null. This source is subscribed when the selector's value has no entry in the sources dictionary; the operator requires an explicit non-null default, so null is rejected when the operator is created.","triggerScenarios":"Calling Case(selector, sources, defaultSource) with defaultSource == null, e.g. passing null intending 'no default', or Empty<TResult>(scheduler) result stored in a nullable that evaluated to null.","commonSituations":"Users expecting a null default to mean 'do nothing' — use Empty<TResult>(scheduler) instead; DI/config failing to provide the default stream; wrong overload chosen (scheduler overload expects IAsyncScheduler, not null observable).","solutions":["Pass Empty<TResult>(scheduler) as defaultSource when no default behavior is wanted.","Provide a real fallback observable that emits an error or completed signal.","If you don't need a default, use the Case(selector, sources, scheduler) overload."],"exampleFix":"// before\nvar stream = Case(selector, sources, null); // meant \"no default\"\n// after\nvar stream = Case(selector, sources, Empty<TResult>(TaskPoolAsyncScheduler.Default));","handlingStrategy":"validation","validationCode":"if (defaultSource is null) defaultSource = Empty<TResult>(TaskPoolAsyncScheduler.Default);","typeGuard":"bool HasDefault<TResult>(IAsyncObservable<TResult>? o) => o is not null;","tryCatchPattern":"try { var s = Case(selector, sources, defaultSource); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"defaultSource\") { /* use Empty<TResult>() */ }","preventionTips":["Use Empty<TResult>(scheduler) rather than null to mean 'no default'","Provide an explicit fallback observable (complete or OnError) for unmatched keys","Prefer the Case(selector, sources, scheduler) overload when no default is needed"],"tags":["asyncrx","argumentnull","case","default-source"],"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"}