{"record":{"id":"e61e336abb59f557","repo":"stride3d/stride","slug":"indices-for-int2-run-from-0-to-1-inclusive","errorCode":null,"errorMessage":"Indices for Int2 run from 0 to 1, inclusive.","messagePattern":"Indices for Int2 run from 0 to 1, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Int2.cs","lineNumber":143,"sourceCode":"        Y = values[1];\n    }\n\n    /// <summary>\n    /// Gets or sets the component at the specified index.\n    /// </summary>\n    /// <value>The value of the X or Y component, depending on the index.</value>\n    /// <param name=\"index\">The index of the component to access. Use 0 for the X component and 1 for the Y component.</param>\n    /// <returns>The value of the component at the specified index.</returns>\n    /// <exception cref=\"System.ArgumentOutOfRangeException\">Thrown when the <paramref name=\"index\"/> is out of the range [0, 1].</exception>\n    public int this[int index]\n    {\n        readonly get\n        {\n            return index switch\n            {\n                0 => X,\n                1 => Y,\n                _ => throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Int2 run from 0 to 1, inclusive.\"),\n            };\n        }\n\n        set\n        {\n            switch (index)\n            {\n                case 0: X = value; break;\n                case 1: Y = value; break;\n                default: throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Int2 run from 0 to 1, inclusive.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Casts from System.Numerics to Stride.Maths vectors\n    /// </summary>\n    /// <param name=\"v\">Value to cast</param>","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Int2.cs#L125-L161","documentation":"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.","triggerScenarios":"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.","commonSituations":"Generic vector-processing code written against Int3/Int4 reused on Int2; data-driven component access where the component count came from user data.","solutions":["Loop with i < 2 for Int2, or guard: if (index >= 0 && index < 2)","Use a component-count constant per vector type instead of hardcoding a shared bound","For generic code, check the vector type before choosing the index bound"],"exampleFix":"// before\nfor (int i = 0; i < 3; i++) sum += v[i]; // throws at i=2\n// after\nfor (int i = 0; i < 2; i++) sum += v[i];","handlingStrategy":"validation","validationCode":"if (index is < 0 or > 1)\n    throw new ArgumentOutOfRangeException(nameof(index));\nvar component = v[index];","typeGuard":"static bool IsValidInt2Index(int i) => (uint)i < 2;","tryCatchPattern":"try { component = v[index]; }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\")\n{\n    component = 0; // or surface a data error\n}","preventionTips":["Hardcode loop bounds per vector type (Int2 -> 2)","Avoid sharing a single component-count constant across Int2/Int3/Int4 code","Prefer named properties X/Y over indexer when the component is known at compile time"],"tags":["csharp","stride","mathematics","indexer","argument-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}