{"record":{"id":"f3ba8f8e7a98f839","repo":"stride3d/stride","slug":"indices-for-vector2-run-from-0-to-1-inclusive","errorCode":null,"errorMessage":"Indices for Vector2 run from 0 to 1, inclusive.","messagePattern":"Indices for Vector2 run from 0 to 1, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Vector2.cs","lineNumber":144,"sourceCode":"\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 float this[int index]\n    {\n        get\n        {\n            switch (index)\n            {\n                case 0: return X;\n                case 1: return Y;\n            }\n\n            throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Vector2 run from 0 to 1, inclusive.\");\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 Vector2 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>\n    public static implicit operator Vector2(System.Numerics.Vector2 v)","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Vector2.cs#L126-L162","documentation":"The Vector2 indexer bounds-checks the index parameter and throws ArgumentOutOfRangeException when the index is not 0 or 1. It is a generic guard on component access: 0 maps to X and 1 to Y; since Vector2 has only two components, any other index is invalid.","triggerScenarios":"Reading v[i] with i >= 2 or i < 0, e.g. a loop written for Vector3/Vector4 iterating 3-4 components of a Vector2.","commonSituations":"Generic shader-like component loops reused across vector types; computed indices from bad data; z-vector/z-buffer code copied from 3D examples.","solutions":["Validate the index is 0 or 1 before indexing.","Bound loops with 2 for Vector2 components.","Access X/Y properties directly when the component is known statically."],"exampleFix":"// before\nfor (int i = 0; i < 3; i++) sum += v[i]; // Vector2 has only 2\n// after\nfor (int i = 0; i < 2; i++) sum += v[i];","handlingStrategy":"validation","validationCode":"if (index < 0 || index > 1)\n    throw new ArgumentOutOfRangeException(nameof(index), \"Vector2 index must be 0 or 1.\");\nvar component = v[index];","typeGuard":"bool IsValidVector2Index(int i) => i == 0 || i == 1;","tryCatchPattern":"try\n{\n    component = v[index];\n}\ncatch (ArgumentOutOfRangeException)\n{\n    component = 0f;\n}","preventionTips":["Use X/Y properties instead of indexer for statically known components.","Bound component loops at 2 for Vector2.","Avoid sharing generic component-iteration helpers across different vector types without a count parameter."],"tags":["mathematics","index-out-of-range","indexer"],"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"}