dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'enumerableFactory')
Error message
Value cannot be null. (Parameter 'enumerableFactory')
What it means
Defer creates a sequence whose factory is invoked on each GetEnumerator call. The library throws ArgumentNullException when enumerableFactory is null, guarding the deferred pipeline at construction time so the failure is attributable to the caller, not a later MoveNext.
Solutions
- Ensure the Func<IEnumerable<TResult>> passed to Defer is non-null
- If using Case/If, verify every branch supplies a valid factory and that the selector always hits a defined branch
- Throw a descriptive exception or substitute Enumerable.Empty<TResult>() when the factory is legitimately absent
Example fix
// before
factories.TryGetValue(key, out Func<IEnumerable<int>> factory);
var seq = EnumerableEx.Defer(factory); // factory may be null
// after
if (factory == null)
throw new KeyNotFoundException($"No sequence factory for '{key}'.");
var seq = EnumerableEx.Defer(factory); Defensive patterns
Strategy: validation
Validate before calling
if (enumerableFactory == null)
throw new ArgumentNullException(nameof(enumerableFactory));
var seq = EnumerableEx.Defer(enumerableFactory); Type guard
static bool IsValidFactory<TResult>(Func<IEnumerable<TResult>> factory) => factory is not null;
Try / catch
try
{
var seq = EnumerableEx.Defer(factory);
}
catch (ArgumentNullException ex) when (ex.ParamName == "enumerableFactory")
{
// fall back to an empty sequence or throw a domain-specific error
} Prevention
- When branching on a key, throw on unknown keys instead of leaving the factory null
- Use exhaustive switch expressions so every branch yields a factory
- Coalesce with () => Enumerable.Empty<TResult>() when absence is legitimate
When it happens
Trigger: Calling EnumerableEx.Defer<TResult>(null) directly, or Case/If operators receiving a null sequence-factory (or a factory map with null entries) that they forward to Defer.
Common situations: A dictionary of branch factories where the requested key is missing and TryGetValue leaves the delegate null, a conditional expression with a null branch, or a lazily-initialized factory never assigned.
Related errors
- Value cannot be null. (Parameter 'defaultSource')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'handler')
- Value cannot be null. (Parameter 'sources')
- Value cannot be null. (Parameter 'first')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/13e91736fa9fb143.
Report an issue: GitHub.
Appendix: source
Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Defer.cs:20
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
/// <summary>
/// Creates an enumerable sequence based on an enumerable factory function.
/// </summary>
/// <typeparam name="TResult">Result sequence element type.</typeparam>
/// <param name="enumerableFactory">Enumerable factory function.</param>
/// <returns>Sequence that will invoke the enumerable factory upon a call to GetEnumerator.</returns>
public static IEnumerable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enumerableFactory)
{
if (enumerableFactory == null)
throw new ArgumentNullException(nameof(enumerableFactory));
return DeferCore(enumerableFactory);
}
private static IEnumerable<TSource> DeferCore<TSource>(Func<IEnumerable<TSource>> enumerableFactory)
{
foreach (var item in enumerableFactory())
{
yield return item;
}
}
}
}
View on GitHub (pinned to 94b5d5ab91)