{"record":{"id":"7a5eb91a9aa24781","repo":"stride3d/stride","slug":"indices-for-vector4-run-from-0-to-3-inclusive","errorCode":null,"errorMessage":"Indices for Vector4 run from 0 to 3, inclusive.","messagePattern":"Indices for Vector4 run from 0 to 3, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Vector4.cs","lineNumber":200,"sourceCode":"\n    /// <summary>\n    /// Gets or sets the component at the specified index.\n    /// </summary>\n    /// <value>The value of the X, Y, Z, or W component, depending on the index.</value>\n    /// <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>\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, 3].</exception>\n    public float this[int index]\n    {\n        readonly get\n        {\n            return index switch\n            {\n                0 => X,\n                1 => Y,\n                2 => Z,\n                3 => W,\n                _ => throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Vector4 run from 0 to 3, inclusive.\"),\n            };\n        }\n\n        set\n        {\n            switch (index)\n            {\n                case 0: X = value; break;\n                case 1: Y = value; break;\n                case 2: Z = value; break;\n                case 3: W = value; break;\n                default: throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Vector4 run from 0 to 3, inclusive.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Casts from System.Numerics to Stride.Maths vectors","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Vector4.cs#L182-L218","documentation":"Vector4's read indexer accepts only indices 0–3 (X, Y, Z, W). Any other index throws ArgumentOutOfRangeException('index') with 'Indices for Vector4 run from 0 to 3, inclusive.' Unlike arrays, the vector has a fixed component count.","triggerScenarios":"Reading vector[i] in a loop bounded by an external length (e.g. iterating 0..values.Length where values has more than 4 entries); computing an index arithmetically and going negative or past 3.","commonSituations":"Generic component-access code written for arrays and reused on Vector4; a component-count constant changed from 4 to something else; deserialization loops using the wrong dimension.","solutions":["Bound index-based loops to 4 (or vector component count), not the source array length","Clamp or validate the index before accessing vector[index]","Access named components X/Y/Z/W directly when the index is known at compile time"],"exampleFix":"// before\nfor (int i = 0; i < rawData.Length; i++)\n    sum += vector[i];\n// after\nfor (int i = 0; i < 4; i++)\n    sum += vector[i];","handlingStrategy":"type-guard","validationCode":"if (index is < 0 or > 3)\n    throw new ArgumentOutOfRangeException(nameof(index));\nvar c = vector[index];","typeGuard":"static bool IsValidComponentIndex(int index) => index is >= 0 and <= 3;","tryCatchPattern":"try\n{\n    value = vector[index];\n}\ncatch (ArgumentOutOfRangeException)\n{\n    value = 0f; // or handle invalid index explicitly\n}","preventionTips":["Bound component loops by 4, not by external array lengths","Prefer named X/Y/Z/W access when the index is compile-time known","Validate data-driven indices before indexing fixed-size vectors"],"tags":["csharp","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-23T08:17:48.524Z"}