stride3d/stride · error · ArgumentOutOfRangeException

Indices for Matrix run from 0 to 15, inclusive.

Error message

Indices for Matrix run from 0 to 15, inclusive.

What it means

Indexer bounds guard on Matrix this[index]: the requested component index is outside [0,15]; the switch over the sixteen M11..M44 entries fell to the default case. The faulting input is the index argument to the indexer.

Solutions

  1. Validate: if (index >= 0 && index < 16) before access
  2. Recheck row/col to linear index math (valid: row*4+col for 0<=row,col<4)
  3. Bound loops to i < 16

Example fix

// before
var m11 = m[(row - 1) * 3 + (col - 1)]; // 3x3 math on 4x4 gives wrong/invalid index
// after
var m11 = m[row * 4 + col]; // valid when 0 <= row, col < 4
Defensive patterns

Strategy: validation

Validate before calling

if (index is < 0 or > 15)
    throw new ArgumentOutOfRangeException(nameof(index));
var value = m[index];

Type guard

static bool IsValidMatrixIndex(int i) => (uint)i < 16;
static int ToMatrixIndex(int row, int col) =>
    (row, col) is { Item1: >= 0 and < 4, Item2: >= 0 and < 4 } ? row * 4 + col : throw new ArgumentOutOfRangeException();

Try / catch

try { value = m[index]; }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index")
{
    // log index and context; use identity/default
}

Prevention

When it happens

Trigger: Reading m[index] with index outside 0..15, e.g. iterating with a wrong bound, negative index, or a linear index computed from row/col arithmetic that overflowed (e.g. row*4+col with row=4).

Common situations: Flattened matrix serialization loops with off-by-one bounds; index math bugs like (row-1)*4+col misapplied; copy-pasted code from 3x3 matrices into 4x4 access.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Matrix.cs:424

            return index switch
            {
                0 => M11,
                1 => M12,
                2 => M13,
                3 => M14,
                4 => M21,
                5 => M22,
                6 => M23,
                7 => M24,
                8 => M31,
                9 => M32,
                10 => M33,
                11 => M34,
                12 => M41,
                13 => M42,
                14 => M43,
                15 => M44,
                _ => throw new ArgumentOutOfRangeException(nameof(index), "Indices for Matrix run from 0 to 15, inclusive."),
            };
        }

        set
        {
            switch (index)
            {
                case 0: M11 = value; break;
                case 1: M12 = value; break;
                case 2: M13 = value; break;
                case 3: M14 = value; break;
                case 4: M21 = value; break;
                case 5: M22 = value; break;
                case 6: M23 = value; break;
                case 7: M24 = value; break;
                case 8: M31 = value; break;
                case 9: M32 = value; break;
                case 10: M33 = value; break;

View on GitHub (pinned to 96fad776d2)