stride3d/stride · error · ArgumentOutOfRangeException

Indices for Vector3 run from 0 to 2, inclusive.

Error message

Indices for Vector3 run from 0 to 2, inclusive.

What it means

Thrown by the Vector3 indexer getter when the index is outside 0..2. The indexer switches on index and returns X, Y, or Z; any other value falls through to ArgumentOutOfRangeException. Vector3 has exactly three components.

Solutions

  1. Clamp or validate the index to 0..2 before accessing the indexer
  2. Fix loop bounds to iterate i < 3 for Vector3
  3. Use a Count/ComponentCount constant instead of a hardcoded 4

Example fix

// before
for (int i = 0; i < 4; i++) sum += v[i];
// after
for (int i = 0; i < 3; i++) sum += v[i];
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > 2) throw new ArgumentOutOfRangeException(nameof(index));

Type guard

static bool IsValidComponentIndex(int index) => (uint)index <= 2u;

Try / catch

try { var c = v[index]; }
catch (ArgumentOutOfRangeException) { /* clamp or handle invalid component index */ }

Prevention

When it happens

Trigger: Accessing vector[index] with index < 0 or index > 2, commonly in loops with a wrong bound (e.g. iterating to vector.Length or 4 for SIMD-sized arrays) or passing a component index computed from data.

Common situations: Generic component-wise loops written for Vector4 reused on Vector3; component index read from config; off-by-one loop conditions.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Vector3.cs:172

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the X, Y, or Z 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, and 2 for the Z 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, 2].</exception>
    public float this[int index]
    {
        get
        {
            switch (index)
            {
                case 0: return X;
                case 1: return Y;
                case 2: return Z;
            }

            throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector3 run from 0 to 2, inclusive.");
        }

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

    /// <summary>
    /// Casts from System.Numerics to Stride.Maths vectors
    /// </summary>
    /// <param name="v">Value to cast</param>

View on GitHub (pinned to 96fad776d2)