dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'resultSelector')
Error message
Value cannot be null. (Parameter 'resultSelector')
What it means
System.ArgumentNullException thrown by the seeded overload with result selector when 'resultSelector' (Func<TAccumulate,TResult>) is null, guarded at Aggregate.cs:68. Notably this overload checks resultSelector before func, so a null resultSelector is reported even if func is also null.
Solutions
- Provide a result selector, e.g. source.Aggregate(seed, acc, l => l.Count).
- If no projection is needed, switch to the overload without resultSelector.
- Substitute an identity-like selector (x => x) if the delegate is conditionally null.
Example fix
// before
var result = source.Aggregate(new List<int>(), (acc, x) => { acc.Add(x); return acc; }, maybeSelector); // null
// after
var result = source.Aggregate(new List<int>(), (acc, x) => { acc.Add(x); return acc; }, maybeSelector ?? (l => (IReadOnlyList<int>)l)); Defensive patterns
Strategy: validation
Validate before calling
if (resultSelector is null) throw new ArgumentNullException(nameof(resultSelector)); var result = source.Aggregate(seed, func, resultSelector);
Type guard
static bool IsValidSelectorArgs<TAccumulate, TResult>(Func<TAccumulate, TResult>? resultSelector) => resultSelector is not null;
Try / catch
try { var result = source.Aggregate(seed, acc, proj); }
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* add identity projection or drop the overload */ } Prevention
- If no projection is needed, use the overload without resultSelector instead of passing null.
- Coalesce with an identity/projection default when the selector is optional.
- Keep projections defined near the aggregation so they are never left unassigned.
When it happens
Trigger: Calling source.Aggregate(seed, func, null) — the three-delegate form with a null final projection. Also produced when both func and resultSelector are null (resultSelector check runs first).
Common situations: Projection delegates kept in nullable members not yet initialized; refactoring that removed the result selector but kept the four-argument call; null passed conditionally for optional projection.
Related errors
- Value cannot be null. (Parameter 'func')
- new ArgumentNullException(nameof(comparer))
- ArgumentNullException: comparer
- Value cannot be null. (Parameter 'selector')
- Value cannot be null. (Parameter 'sources')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/4cb6a1b39df47227.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs:68
public static IAsyncObservable<TResult> Aggregate<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, ValueTask<TResult>> func)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (func == null)
throw new ArgumentNullException(nameof(func));
return CreateAsyncObservable<TResult>.From(
source,
(seed, func),
static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, state.seed, state.func)));
}
public static IAsyncObservable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (resultSelector == null)
throw new ArgumentNullException(nameof(resultSelector));
if (func == null)
throw new ArgumentNullException(nameof(func));
return CreateAsyncObservable<TResult>.From(
source,
(seed, func, resultSelector),
static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, state.seed, state.func, state.resultSelector)));
}
public static IAsyncObservable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> func, Func<TAccumulate, ValueTask<TResult>> resultSelector)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (resultSelector == null)
throw new ArgumentNullException(nameof(resultSelector));
if (func == null)
throw new ArgumentNullException(nameof(func));
View on GitHub (pinned to 94b5d5ab91)