{"record":{"id":"0560befc8c5a933b","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-todictionary","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToDictionary.cs","lineNumber":15,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<IDictionary<TKey, TValue>> ToDictionary<TSource, TKey, TValue>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (valueSelector == null)\n                throw new ArgumentNullException(nameof(valueSelector));\n\n            return CreateAsyncObservable<IDictionary<TKey, TValue>>.From(\n                source,\n                (keySelector, valueSelector),\n                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.ToDictionary(observer, state.keySelector, state.valueSelector)));\n        }\n\n        public static IAsyncObservable<IDictionary<TKey, TValue>> ToDictionary<TSource, TKey, TValue>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector, IEqualityComparer<TKey> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (valueSelector == null)","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToDictionary.cs#L1-L33","documentation":"ToDictionary(source, keySelector, valueSelector) throws ArgumentNullException for a null source at line 15, before validating the selectors. The operator aggregates the source sequence into an IDictionary<TKey, TValue>, so a null source observable cannot be subscribed. The library guards every public operator argument eagerly.","triggerScenarios":"Calling source.ToDictionary(keySelector, valueSelector) with source == null.","commonSituations":"Composing query chains where an earlier operator or factory returned null; config-driven pipelines with a missing source entry.","solutions":["Ensure the IAsyncObservable<TSource> is created and non-null before the call.","Fix the null-producing factory or field upstream.","Substitute an empty async observable when the source may be absent."],"exampleFix":"// before\nvar dict = src.ToDictionary(x => x.Key, x => x.Val); // src == null\n// after\nvar s = src ?? CreateAsyncObservable<int>.Empty();\nvar dict = s.ToDictionary(x => x.Key, x => x.Val);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentException(\"source required\");\nvar dict = source.ToDictionary(keySelector, valueSelector);","typeGuard":"static bool CanBuildDict<T, K, V>(IAsyncObservable<T> s, Func<T, K> k, Func<T, V> v) => s is not null && k is not null && v is not null;","tryCatchPattern":"try { var d = source.ToDictionary(ks, vs); }\ncatch (ArgumentNullException ex) when (ex.ParamName is \"source\" or \"keySelector\" or \"valueSelector\") { /* fix args */ }","preventionTips":["Check the whole operator chain for null early results","Guard pipeline builders that assemble sources from config","Prefer throwing over returning null in factory methods"],"tags":["csharp","async-rx","argument-null","operator"],"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"}