stride3d/stride · error · ArgumentOutOfRangeException

Indices for Int4 run from 0 to 3, inclusive.

Error message

Indices for Int4 run from 0 to 3, inclusive.

What it means

The Int4 indexer bounds-checks the index parameter and throws ArgumentOutOfRangeException when the index is outside [0, 3]. It is a generic guard on component access: 0 selects X, 1 Y, 2 Z, 3 W; any negative or >= 4 index is rejected.

Solutions

  1. Bound loops to i < 4 for Int4
  2. Validate: if ((uint)index < 4) before access
  3. For type-generic code, map the component count per vector type

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

static bool IsValidInt4Index(int i) => (uint)i < 4;

Try / catch

try { component = v[index]; }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index")
{
    component = 0; // or surface a data error
}

Prevention

When it happens

Trigger: Reading v[index] with index outside 0..3, e.g. loops bounded by a larger count, negative indices, or indices computed from quaternion/color metadata with more components.

Common situations: Generic component iteration over mixed vector types; reflection-driven property access with bad ordinal mapping.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Int4.cs:156

        W = values[3];
    }

    /// <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 = "ArgumentOutOfRangeException">Thrown when the <paramref name = "index" /> is out of the range [0, 3].</exception>
    public int this[int index]
    {
        readonly get => index switch
        {
            0 => X,
            1 => Y,
            2 => Z,
            3 => W,
            _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Int4 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;

View on GitHub (pinned to 96fad776d2)