stride3d/stride · error · ArgumentOutOfRangeException

Indices for Double3 run from 0 to 2, inclusive.

Error message

Indices for Double3 run from 0 to 2, inclusive.

What it means

Double3's read-only indexer maps index 0->X, 1->Y, 2->Z and throws ArgumentOutOfRangeException for any other index. This surfaces when iterating the vector with a wrong bound or using an off-by-one loop limit.

Solutions

  1. Loop with 'for (int i = 0; i < 3; i++)' when iterating Double3 components
  2. Access X, Y, Z fields directly instead of the indexer when the component is known at compile time
  3. If the component count varies, check the vector type/dimension before looping

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 < 3) { var c = v[index]; } else { /* handle invalid index */ }

Type guard

static bool IsValidComponentIndex(int i) => i is >= 0 and <= 2;

Try / catch

try { var c = v[index]; }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index") { /* clamp index or log bad accessor */ }

Prevention

When it happens

Trigger: Reading v[index] with index < 0 or index > 2, e.g. 'for (int i = 0; i <= 3; i++) v[i]' or indexing with an enum/variable that holds an invalid value.

Common situations: Generic per-component loops written for 4-component types (Double4) reused on Double3; serializing components with a hardcoded component count of 4.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Double3.cs:154

    }

    /// <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 double this[int index]
    {
        readonly get
        {
            return index switch
            {
                0 => X,
                1 => Y,
                2 => Z,
                _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double3 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 Double3 run from 0 to 2, inclusive.");
            }
        }
    }

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

View on GitHub (pinned to 96fad776d2)