stride3d/stride · error · ArgumentOutOfRangeException

Indices for Vector4 run from 0 to 3, inclusive.

Error message

Indices for Vector4 run from 0 to 3, inclusive.

What it means

Vector4's read indexer accepts only indices 0–3 (X, Y, Z, W). Any other index throws ArgumentOutOfRangeException('index') with 'Indices for Vector4 run from 0 to 3, inclusive.' Unlike arrays, the vector has a fixed component count.

Solutions

  1. Bound index-based loops to 4 (or vector component count), not the source array length
  2. Clamp or validate the index before accessing vector[index]
  3. Access named components X/Y/Z/W directly when the index is known at compile time

Example fix

// before
for (int i = 0; i < rawData.Length; i++)
    sum += vector[i];
// after
for (int i = 0; i < 4; i++)
    sum += vector[i];
Defensive patterns

Strategy: type-guard

Validate before calling

if (index is < 0 or > 3)
    throw new ArgumentOutOfRangeException(nameof(index));
var c = vector[index];

Type guard

static bool IsValidComponentIndex(int index) => index is >= 0 and <= 3;

Try / catch

try
{
    value = vector[index];
}
catch (ArgumentOutOfRangeException)
{
    value = 0f; // or handle invalid index explicitly
}

Prevention

When it happens

Trigger: Reading vector[i] in a loop bounded by an external length (e.g. iterating 0..values.Length where values has more than 4 entries); computing an index arithmetically and going negative or past 3.

Common situations: Generic component-access code written for arrays and reused on Vector4; a component-count constant changed from 4 to something else; deserialization loops using the wrong dimension.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Vector4.cs:200

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the X, Y, Z, or W component, depending on the index.</value>
    /// <param name="index">The index of the component to access. Use 0 for the X component, 1 for the Y component, 2 for the Z component, and 3 for the W component.</param>
    /// <returns>The value of the component at the specified index.</returns>
    /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 3].</exception>
    public float this[int index]
    {
        readonly get
        {
            return index switch
            {
                0 => X,
                1 => Y,
                2 => Z,
                3 => W,
                _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector4 run from 0 to 3, inclusive."),
            };
        }

        set
        {
            switch (index)
            {
                case 0: X = value; break;
                case 1: Y = value; break;
                case 2: Z = value; break;
                case 3: W = value; break;
                default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector4 run from 0 to 3, inclusive.");
            }
        }
    }

    /// <summary>
    /// Casts from System.Numerics to Stride.Maths vectors

View on GitHub (pinned to 96fad776d2)