dotnet/reactive · error · ArgumentNullException

Thrown when elseSource is null (ArgumentNullException…

Error message

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

What it means

The three-argument If operator requires elseSource as the branch returned when the condition is false. A null elseSource throws ArgumentNullException with param name 'elseSource' at call time. Unlike LINQ's standard Where-style APIs, Ix.NET makes even the else branch mandatory — use the two-argument overload for an implicit empty else.

Solutions

  1. Use the two-argument overload If(condition, thenSource), which returns an empty sequence when false.
  2. Pass Enumerable.Empty<TResult>() as the else branch.
  3. Fix the provider that should supply the fallback sequence.

Example fix

// before
var seq = GeneratorEx.If(cond, thenSrc, null);
// after
var seq = GeneratorEx.If(cond, thenSrc); // empty sequence when cond is false
Defensive patterns

Strategy: validation

Validate before calling

// The else branch is NOT optional in the 3-arg overload; if you want empty, use the 2-arg overload:
if (elseSource is null) { var seq = GeneratorEx.If(condition, thenSource); }

Type guard

bool HasElseSource<TResult>(IEnumerable<TResult>? s) => s is not null;

Try / catch

try { var seq = GeneratorEx.If(condition, thenSource, elseSource); }
catch (ArgumentNullException ex) when (ex.ParamName == "elseSource") { log.Error("else branch missing; use the 2-arg overload for an empty else"); }

Prevention

When it happens

Trigger: Calling If(condition, thenSource, null) — commonly when the developer assumed the else branch was optional, or a fallback sequence variable was null.

Common situations: Migrating from the 2-arg If overload and passing null 'for empty'; default/fallback sequences not yet configured; optional configuration sections providing the else sequence.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs:26

{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Returns an enumerable sequence based on the evaluation result of the given condition.
        /// </summary>
        /// <typeparam name="TResult">Result sequence element type.</typeparam>
        /// <param name="condition">Condition to evaluate.</param>
        /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
        /// <param name="elseSource">Sequence to return in case the condition evaluates false.</param>
        /// <returns>Either of the two input sequences based on the result of evaluating the condition.</returns>
        public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (thenSource == null)
                throw new ArgumentNullException(nameof(thenSource));
            if (elseSource == null)
                throw new ArgumentNullException(nameof(elseSource));

            return Defer(() => condition() ? thenSource : elseSource);
        }

        /// <summary>
        /// Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty
        /// sequence.
        /// </summary>
        /// <typeparam name="TResult">Result sequence element type.</typeparam>
        /// <param name="condition">Condition to evaluate.</param>
        /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
        /// <returns>The given input sequence if the condition evaluates true; otherwise, an empty sequence.</returns>
        public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));
            if (thenSource == null)
                throw new ArgumentNullException(nameof(thenSource));

View on GitHub (pinned to 94b5d5ab91)