dotnet/reactive · error · ArgumentNullException

ArgumentNullException (default message: Value cannot be…

Error message

ArgumentNullException (default message: Value cannot be null. (Parameter 'values'))

What it means

StartWith prepends the given values to the source; the values array is validated because the pre-emission queue cannot be built from null. Fix: pass an empty array (or use StartWithEmpty-style behavior) instead of null.

Solutions

  1. Pass an empty array instead of null (`Array.Empty<TSource>()`).
  2. Normalize null to empty at the helper boundary before forwarding.
  3. Fix the loader that produced a null values list.
  4. Use the StartWith(IEnumerable<TSource>) overload with an empty collection.

Example fix

// before
public IObservable<int> Build(IObservable<int> src, int[] prefix) => src.StartWith(prefix);
// after
public IObservable<int> Build(IObservable<int> src, int[]? prefix) => src.StartWith(prefix ?? Array.Empty<int>());
Defensive patterns

Strategy: validation

Validate before calling

if (values is null) throw new ArgumentNullException(nameof(values));
var withPrefix = source.StartWith(values);

Type guard

bool HasValues<TSource>(TSource[]? v) => v is not null;

Try / catch

try { withPrefix = source.StartWith(vals); }
catch (ArgumentNullException ex) when (ex.ParamName == "values") { withPrefix = source; }

Prevention

When it happens

Trigger: Calling StartWith(source, values) with a null params array, e.g. passing a null TSource[] variable or forwarding a null from another params call.

Common situations: Forwarding optional prelude values through helper methods where null means 'no prefix' instead of an empty array; config-driven value lists that failed to load.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Single.cs:797

        /// <summary>
        /// Prepends a sequence of values to an observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence to prepend values to.</param>
        /// <param name="values">Values to prepend to the specified sequence.</param>
        /// <returns>The source sequence prepended with the specified values.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="values"/> is null.</exception>
        public static IObservable<TSource> StartWith<TSource>(this IObservable<TSource> source, params TSource[] values)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            return s_impl.StartWith(source, values);
        }

        /// <summary>
        /// Prepends a sequence of values to an observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence to prepend values to.</param>
        /// <param name="values">Values to prepend to the specified sequence.</param>
        /// <returns>The source sequence prepended with the specified values.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="values"/> is null.</exception>
        public static IObservable<TSource> StartWith<TSource>(this IObservable<TSource> source, IEnumerable<TSource> values)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));

View on GitHub (pinned to 94b5d5ab91)