{"record":{"id":"9f3c5ab6f24f54dc","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-valueselector","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'valueSelector')","messagePattern":"Value cannot be null\\. \\(Parameter 'valueSelector'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToDictionary.cs","lineNumber":19,"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)\n                throw new ArgumentNullException(nameof(valueSelector));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToDictionary.cs#L1-L37","documentation":"System.Reactive.Async's ToDictionary extension validates all arguments up front and throws ArgumentNullException when the valueSelector Func<TSource,TValue> is null. The library eagerly guards operators so failures surface at call time rather than during async subscription. Passing a null valueSelector means the operator could never project values into the dictionary.","triggerScenarios":"Calling source.ToDictionary(keySelector, null) — the 2-argument synchronous-selector overload at ToDictionary.cs:19 (e.g. source.ToDictionary(x => x.Id, null)).","commonSituations":"Building the selector dynamically from configuration or reflection and getting null; refactoring that removed a lambda but left the argument; conditional logic like Func<TSource,TValue> sel = cond ? x => x.Val : null.","solutions":["Pass a non-null value selector lambda, e.g. source.ToDictionary(x => x.Key, x => x.Value).","If the selector is optional, supply a default such as x => x before calling.","If selection can fail, throw a meaningful exception inside the lambda instead of passing null."],"exampleFix":"// before\nvar dict = await source.ToDictionary(x => x.Id, (Func<Order, decimal>)null);\n// after\nvar dict = await source.ToDictionary(x => x.Id, x => x.Total);","handlingStrategy":"validation","validationCode":"if (source == null) throw new ArgumentNullException(nameof(source));\nif (keySelector == null) throw new ArgumentNullException(nameof(keySelector));\nif (valueSelector == null) throw new ArgumentNullException(nameof(valueSelector));\nvar dict = await source.ToDictionary(keySelector, valueSelector);","typeGuard":"static bool IsValidSelectors<TSource,TKey,TValue>(Func<TSource,TKey> k, Func<TSource,TValue> v) => k != null && v != null;","tryCatchPattern":"try { var dict = await source.ToDictionary(keySelector, valueSelector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"valueSelector\") { /* supply default selector or log */ }","preventionTips":["Never assign null to Func/ValueTask delegate variables; default to identity lambdas.","Let the compiler infer lambdas inline instead of casting null to delegate types.","Add Debug.Assert(selector != null) where selectors are built dynamically."],"tags":["argumentnull","asyncrx","linq-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-23T08:17:48.524Z"}