stride3d/stride · error · ArgumentOutOfRangeException

Indices for Int2 run from 0 to 1, inclusive.

Error message

Indices for Int2 run from 0 to 1, inclusive.

What it means

Int2 exposes only two components, so its read indexer accepts index 0 (X) or 1 (Y) only. Any other index throws ArgumentOutOfRangeException. This is a bounds check on the logical component index, not an array access.

Solutions

  1. Loop with i < 2 for Int2, or guard: if (index >= 0 && index < 2)
  2. Use a component-count constant per vector type instead of hardcoding a shared bound
  3. For generic code, check the vector type before choosing the index bound

Example fix

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

Strategy: validation

Validate before calling

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

Type guard

static bool IsValidInt2Index(int i) => (uint)i < 2;

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] in a loop with the wrong bound (e.g. for i < 3 or i < componentCount of a larger vector type), or passing a dynamic index computed from another vector's dimension.

Common situations: Generic vector-processing code written against Int3/Int4 reused on Int2; data-driven component access where the component count came from user data.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Int2.cs:143

        Y = values[1];
    }

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the X or Y component, depending on the index.</value>
    /// <param name="index">The index of the component to access. Use 0 for the X component and 1 for the Y 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, 1].</exception>
    public int this[int index]
    {
        readonly get
        {
            return index switch
            {
                0 => X,
                1 => Y,
                _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Int2 run from 0 to 1, inclusive."),
            };
        }

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

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

View on GitHub (pinned to 96fad776d2)