dotnet/reactive · error · ArgumentNullException
Thrown when thenSource is null (ArgumentNullException…
Error message
Thrown when thenSource is null (ArgumentNullException, param name: thenSource)
What it means
The three-argument If operator requires thenSource as the branch returned when the condition is true. It is validated eagerly; a null thenSource throws ArgumentNullException with param name 'thenSource'.
Solutions
- Pass an actual sequence for the true branch, or use the two-argument overload and supply Enumerable.Empty<TResult>() explicitly.
- Coalesce: thenSource ?? Enumerable.Empty<TResult>().
- Fix the source that should provide the branch sequence.
Example fix
// before var seq = GeneratorEx.If(cond, thenSrc, elseSrc); // thenSrc is null // after var seq = GeneratorEx.If(cond, thenSrc ?? Enumerable.Empty<int>(), elseSrc);
Defensive patterns
Strategy: validation
Validate before calling
if (thenSource is null) thenSource = Enumerable.Empty<TResult>();
Type guard
bool HasThenSource<TResult>(IEnumerable<TResult>? s) => s is not null;
Try / catch
try { var seq = GeneratorEx.If(condition, thenSource, elseSource); }
catch (ArgumentNullException ex) when (ex.ParamName == "thenSource") { log.Error("true-branch sequence missing"); } Prevention
- Coalesce branch sequences with ?? Enumerable.Empty<TResult>().
- Ensure branch-producing methods return empty rather than null.
- Validate all branch sources when building conditional pipelines.
When it happens
Trigger: Calling If(condition, null, elseSource) — e.g. the 'true' branch sequence is null because a query method, cache lookup, or configuration-provided sequence was absent.
Common situations: Branch sequences composed from optional data sources; dictionary of named sequences where the key lookup missed; refactors replacing a default sequence with a nullable variable.
Related errors
- Thrown when condition is null (ArgumentNullException, param…
- Thrown when elseSource is null (ArgumentNullException…
- ArgumentNullException: comparer
- nameof(condition)
- nameof(iterate)
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/72599c55e1c93db0.
Report an issue: GitHub.
Appendix: source
Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/If.cs:24
namespace System.Linq
{
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));View on GitHub (pinned to 94b5d5ab91)