stride3d/stride · error · ArgumentOutOfRangeException

Indices for Quaternion run from 0 to 3, inclusive.

Error message

Indices for Quaternion run from 0 to 3, inclusive.

What it means

The Quaternion indexer bounds-checks the index parameter and throws ArgumentOutOfRangeException when the index is outside [0, 3]. It is a generic guard on component access mapping 0-3 to X, Y, Z, W; any other index (negative or >= 4) fires the error.

Solutions

  1. Validate the index is 0..3 before quaternion[index]
  2. Correct loop bounds to iterate exactly 4 components
  3. Access X, Y, Z, W properties directly instead of the indexer

Example fix

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

Strategy: validation

Validate before calling

if (index >= 0 && index <= 3) { var c = quaternion[index]; }

Type guard

bool IsValidQuaternionIndex(int i) => (uint)i <= 3u;

Try / catch

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

Prevention

When it happens

Trigger: Reading quaternion[idx] with idx < 0 or idx > 3, usually from a loop with incorrect bounds or an index computed from other data sizes.

Common situations: Component loops copied from vector code with different component counts; off-by-one iterations; generic math utilities indexing blindly.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Quaternion.cs:254

    /// 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]
    {
        get
        {
            switch (index)
            {
                case 0: return X;
                case 1: return Y;
                case 2: return Z;
                case 3: return W;
            }

            throw new ArgumentOutOfRangeException(nameof(index), "Indices for Quaternion 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 Quaternion run from 0 to 3, inclusive.");
            }
        }
    }

    /// <summary>
    /// Conjugates the quaternion.
    /// </summary>

View on GitHub (pinned to 96fad776d2)