stride3d/stride · error · InvalidOperationException

Enumerables do not have the same number of items.

Error message

Enumerables do not have the same number of items.

What it means

DesignExtensions.Zip(IEnumerable<T>, IEnumerable<T>) throws InvalidOperationException when the two enumerables do not yield the same number of items. As it walks both enumerators in lockstep, a length mismatch is detected when one enumerator can advance and the other cannot.

Solutions

  1. Validate both collections have equal Count before zipping
  2. Pad or truncate the longer sequence to match
  3. Consider .NET's built-in Enumerable.Zip which stops at the shorter sequence if pairing is optional

Example fix

// before
var pairs = keys.Zip(values); // throws if counts differ
// after
if (keys.Count() != values.Count()) throw new InvalidOperationException("keys/values length mismatch");
var pairs = keys.Zip(values);
Defensive patterns

Strategy: validation

Validate before calling

if (keys.Count() != values.Count())
    throw new InvalidOperationException("Sequences must have equal length before Zip.");
var pairs = keys.Zip(values);

Prevention

When it happens

Trigger: Calling Zip on two sequences of unequal length, e.g. zipping a list of keys with a list of values where one collection is shorter.

Common situations: Misaligned data loaded from files/config where one column has more entries, refactoring that changed one collection but not its counterpart, partial deserialization.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/7a53e1b42bb33f7c. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Design/Extensions/DesignExtensions.cs:86

        ArgumentNullException.ThrowIfNull(enumerable2);
#else
        if (enumerable1 is null) throw new ArgumentNullException(nameof(enumerable1));
        if (enumerable2 is null) throw new ArgumentNullException(nameof(enumerable2));
#endif

        return Impl();

        IEnumerable<Tuple<T1, T2>> Impl()
        {
            using var enumerator1 = enumerable1.GetEnumerator();
            using var enumerator2 = enumerable2.GetEnumerator();
            var enumMoved = true;
            while (enumMoved)
            {
                enumMoved = enumerator1.MoveNext();
                var enum2Moved = enumerator2.MoveNext();
                if (enumMoved != enum2Moved)
                    throw new InvalidOperationException("Enumerables do not have the same number of items.");

                if (enumMoved)
                {
                    yield return Tuple.Create(enumerator1.Current, enumerator2.Current);
                }
            }
        }
    }

    /// <summary>
    /// Iterates over all elements of source and their children recursively.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source">The source.</param>
    /// <param name="childrenSelector">The children selector.</param>
    /// <returns></returns>
    [Pure]
    public static IEnumerable<T> SelectDeep<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector)

View on GitHub (pinned to 96fad776d2)