dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'source')
Error message
Value cannot be null. (Parameter 'source')
What it means
StartWith is an extension method that prefixes a sequence with the given values. The library eagerly validates its `source` parameter at call time and throws ArgumentNullException naming `source` when a caller passes null. All argument validation happens when the operator is invoked, not when the result is enumerated.
Solutions
- Ensure the sequence passed to StartWith is non-null before calling (use `?? Array.Empty<TSource>()`).
- Check the upstream call that produced the source; make it return an empty sequence instead of null.
- If null legitimately means 'no data', skip the operator and return a default sequence.
- Wrap in ArgumentNullException-handling only at API boundaries; otherwise fix the null at the source.
Example fix
// before var result = maybeNullItems.StartWith(0); // after var result = (maybeNullItems ?? Array.Empty<int>()).StartWith(0);
Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new ArgumentNullException(nameof(source)); // or coalesce: var src = source ?? Array.Empty<TSource>();
Type guard
static bool IsNonNullSeq<T>(IEnumerable<T> s) => s is not null;
Try / catch
try { var r = input.StartWith(0); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* handle null source */ } Prevention
- Enable nullable reference types (NRT) so null sources are flagged at compile time.
- Coalesce optional sequences at ingestion points.
- Avoid methods that return null sequences; return empty collections.
- Validate inputs at API boundaries before building LINQ pipelines.
When it happens
Trigger: Calling `null.StartWith(1, 2)` or `StartWith(someNullableCollection, 1)` where the IEnumerable<TSource> argument is null; e.g. chaining off a method that can return null or an uninitialized field.
Common situations: Chaining off a nullable LINQ result (FirstOrDefault-style helpers or API calls that return null for 'no data'), uninitialized backing fields, or deserialized objects with null collection properties passed straight into the operator.
Related errors
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'exception')
- Value cannot be null. (Parameter 'resourceFactory')
- Value cannot be null. (Parameter 'enumerableFactory')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/8bf080e9b0f4f753.
Report an issue: GitHub.
Appendix: source
Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/StartsWith.cs:21
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
/// <summary>
/// Returns the source sequence prefixed with the specified value.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="values">Values to prefix the sequence with.</param>
/// <returns>Sequence starting with the specified prefix value, followed by the source sequence.</returns>
public static IEnumerable<TSource> StartWith<TSource>(this IEnumerable<TSource> source, params TSource[] values)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return StartWithCore(source, values);
}
private static IEnumerable<TSource> StartWithCore<TSource>(IEnumerable<TSource> source, params TSource[] values)
{
foreach (var x in values)
{
yield return x;
}
foreach (var item in source)
{
yield return item;
}
}
}
}View on GitHub (pinned to 94b5d5ab91)