dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

TakeLast returns the final `count` elements of a sequence. The operator validates `source` eagerly when the method is called and throws ArgumentNullException naming `source` if it is null. This is fail-fast design: the error surfaces at the operator call site, not during MoveNext.

Solutions

  1. Coalesce the source: `(source ?? Enumerable.Empty<T>()).TakeLast(n)`.
  2. Fix the producer so it never returns null for a sequence.
  3. Return early when the source is null instead of building the query.
  4. Log/inspect the upstream path if null is unexpected — the bug is usually upstream, not in TakeLast.

Example fix

// before
var last3 = possiblyNull.TakeLast(3);
// after
var last3 = (possiblyNull ?? Enumerable.Empty<int>()).TakeLast(3);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));
// or: var safe = source ?? Enumerable.Empty<T>();

Type guard

static bool HasSource<T>(IEnumerable<T> s) => s is not null;

Try / catch

try { var last = seq.TakeLast(n); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* fall back to empty */ }

Prevention

When it happens

Trigger: Calling `TakeLast(null, 5)` or `someNullableSeq.TakeLast(5)` where the source IEnumerable<TSource> is null.

Common situations: Feeding results of a lookup method that returns null on miss into TakeLast, unguarded nullable fields, or LINQ chains built from external data (files, APIs, DB) where a missing entity maps to null.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/TakeLast.cs:22

using System.Collections.Generic;

namespace System.Linq
{
    public static partial class EnumerableEx
    {
#if !(REFERENCE_ASSEMBLY && (NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER ))
        /// <summary>
        /// Returns a specified number of contiguous elements from the end of the sequence.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="count">The number of elements to take from the end of the sequence.</param>
        /// <returns>Sequence with the specified number of elements counting from the end of the source sequence.</returns>
        public static IEnumerable<TSource> TakeLast<TSource>(this IEnumerable<TSource> source, int count)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (count < 0)
                throw new ArgumentOutOfRangeException(nameof(count));

            return TakeLastCore(source, count);
        }

        private static IEnumerable<TSource> TakeLastCore<TSource>(IEnumerable<TSource> source, int count)
        {
            if (count == 0)
            {
                yield break;
            }

            var q = new Queue<TSource>(count);

            foreach (var item in source)
            {
                if (q.Count >= count)

View on GitHub (pinned to 94b5d5ab91)