{"record":{"id":"e51bb077c437b9cd","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-collectionselector","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'collectionSelector')","messagePattern":"Value cannot be null\\. \\(Parameter 'collectionSelector'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/SelectMany.cs","lineNumber":164,"sourceCode":"            return CreateAsyncObservable<TResult>.From(\n                source,\n                (collectionSelector, resultSelector),\n                static async (source, state, observer) =>\n                {\n                    var (sink, inner) = AsyncObserver.SelectMany(observer, state.collectionSelector, state.resultSelector);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, inner);\n                });\n        }\n\n        public static IAsyncObservable<TResult> SelectMany<TSource, TCollection, TResult>(this IAsyncObservable<TSource> source, Func<TSource, int, ValueTask<IAsyncObservable<TCollection>>> collectionSelector, Func<TSource, int, TCollection, int, ValueTask<TResult>> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (collectionSelector == null)\n                throw new ArgumentNullException(nameof(collectionSelector));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return CreateAsyncObservable<TResult>.From(\n                source,\n                (collectionSelector, resultSelector),\n                static async (source, state, observer) =>\n                {\n                    var (sink, inner) = AsyncObserver.SelectMany(observer, state.collectionSelector, state.resultSelector);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, inner);\n                });\n        }\n    }\n\n    public partial class AsyncObserver","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/SelectMany.cs#L146-L182","documentation":"SelectMany (fully async overload) throws ArgumentNullException('collectionSelector') because the ValueTask-returning collectionSelector delegate was null. The library validates all arguments eagerly at the SelectMany call site, so this surfaces immediately rather than when the first element is processed. Supply a valid async collection selector to construct the operator.","triggerScenarios":"Calling SelectMany<TSource,TCollection,TResult>(source, collectionSelector, resultSelector) where collectionSelector == null; common when the inner-observable factory is resolved from DI/config and the resolution returned null.","commonSituations":"Configuration-driven pipelines where the selector is optional and unset; refactors that swapped Task-returning lambdas for ValueTask ones and dropped the argument; mocks returning null delegates.","solutions":["Provide a non-null Func<TSource,int,ValueTask<IAsyncObservable<TCollection>>> collectionSelector","If inner sequences are optional, pass an async lambda returning an empty observable","Null-check the resolved delegate at composition time and fail with context"],"exampleFix":"// before\nvar result = source.SelectMany(_selectorResolver(), rs); // resolver returned null\n// after\nvar cs = _selectorResolver() ?? (async (x, i) => ValueTask.FromResult(AsyncObservable.Empty<TCollection>()));\nvar result = source.SelectMany(cs, rs);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nif (collectionSelector is null) throw new ArgumentNullException(nameof(collectionSelector));\nif (resultSelector is null) throw new ArgumentNullException(nameof(resultSelector));","typeGuard":"static bool IsValidAsyncCollectionSelector<TSource,TCollection>(Func<TSource,int,ValueTask<IAsyncObservable<TCollection>>>? cs) => cs is not null;","tryCatchPattern":"try { var result = source.SelectMany(asyncCs, asyncRs); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"collectionSelector\")\n{ /* resolve/supply the async collection selector */ }","preventionTips":["Guard DI/config-resolved delegates with ?? empty-observable fallbacks","Prefer method groups over nullable delegate lookups","Test ValueTask overload call sites explicitly after migrations","Fail loudly with context when a delegate resolver returns null"],"tags":["csharp","asyncrx","null-check","valuetask"],"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"}