stride3d/stride · error · ArgumentOutOfRangeException

Indices for Double4 run from 0 to 3, inclusive.

Error message

Indices for Double4 run from 0 to 3, inclusive.

What it means

Thrown by the Double4 indexer's getter when the index is outside 0..3. Double4 has exactly four components (0=X, 1=Y, 2=Z, 3=W); any other index is invalid. Thrown as ArgumentOutOfRangeException for the 'index' parameter.

Solutions

  1. Clamp or validate the index to 0..3 before indexing
  2. Fix loop bounds to use 'i < 4' or a component-count constant of 4
  3. If the component count varies, use the array-backed path instead of fixed-size Double4 indexing

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { var c = d4[index]; }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index")
{
    // handle invalid component index
}

Prevention

When it happens

Trigger: Calling d4[i] (get) with i < 0 or i > 3, typically from a loop with a wrong bound (e.g. i <= 4) or an index taken from unvalidated external data.

Common situations: Generic 'copy N components' loops with off-by-one bounds; mapping external component IDs or channel numbers directly to vector indices; iterating to Length of a differently-sized array.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Double4.cs:186

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

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

View on GitHub (pinned to 96fad776d2)