dotnet/reactive · error · ArgumentNullException

Thrown when onNext is null (ArgumentNullException, param…

Error message

Thrown when onNext is null (ArgumentNullException, param name: onNext)

What it means

Documented null-guard failure in EnumerableEx.ForEach: the onNext callback that should be invoked for each enumerated element was null, so the enumeration cannot perform its per-element side effect and throws ArgumentNullException with parameter name 'onNext' before iterating.

Solutions

  1. Pass a concrete Action<TSource>; use _ => { } if no work is needed per element.
  2. Ensure the callback is assigned/subscribed before calling ForEach.
  3. Default the delegate at acquisition: onNext ?? (_ => { }).

Example fix

// before
source.ForEach(handler); // handler may be null
// after
source.ForEach(handler ?? (_ => { }));
Defensive patterns

Strategy: validation

Validate before calling

if (onNext is null) throw new ArgumentNullException(nameof(onNext));
source.ForEach(onNext ?? (_ => { }));

Type guard

bool HasHandler<TSource>(Action<TSource> a) => a is not null;

Try / catch

try { source.ForEach(action); }
catch (ArgumentNullException ex) when (ex.ParamName == "onNext") { /* attach a handler first */ }

Prevention

When it happens

Trigger: Calling ForEach(source, null) — e.g. a callback resolved from an optional member or event handler that is not attached.

Common situations: Handler not yet subscribed; passing a delegate from configuration that is absent; obj?.Handler producing null.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/55b2b5228a9d8bb6. Report an issue: GitHub.

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/ForEach.cs:22

using System.Collections.Generic;

namespace System.Linq
{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Enumerates the sequence and invokes the given action for each value in the sequence.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="onNext">Action to invoke for each element.</param>
        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (onNext == null)
                throw new ArgumentNullException(nameof(onNext));

            foreach (var item in source)
            {
                onNext(item);
            }
        }

        /// <summary>
        /// Enumerates the sequence and invokes the given action for each value in the sequence.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="onNext">Action to invoke for each element.</param>
        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> onNext)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (onNext == null)

View on GitHub (pinned to 94b5d5ab91)