{"record":{"id":"1c425893ee5a1db8","repo":"stride3d/stride","slug":"indices-for-color4-run-from-0-to-3-inclusive","errorCode":null,"errorMessage":"Indices for Color4 run from 0 to 3, inclusive.","messagePattern":"Indices for Color4 run from 0 to 3, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Color4.cs","lineNumber":243,"sourceCode":"\n    /// <summary>\n    /// Gets or sets the component at the specified index.\n    /// </summary>\n    /// <value>The value of the red, green, blue, and alpha components, depending on the index.</value>\n    /// <param name=\"index\">The index of the component to access. Use 0 for the alpha component, 1 for the red component, 2 for the green component, and 3 for the blue 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 => R,\n                1 => G,\n                2 => B,\n                3 => A,\n                _ => throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Color4 run from 0 to 3, inclusive.\"),\n            };\n        }\n\n        set\n        {\n            switch (index)\n            {\n                case 0: R = value; break;\n                case 1: G = value; break;\n                case 2: B = value; break;\n                case 3: A = value; break;\n                default: throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Color4 run from 0 to 3, inclusive.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Converts the color into a packed integer.","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Color4.cs#L225-L261","documentation":"The Color4 indexer performs a bounds check on the index parameter and throws ArgumentOutOfRangeException whenever the index is outside [0, 3]. It is a generic guard on component access: any subscript other than 0 (red), 1 (green), 2 (blue) or 3 (alpha) fires it — Color4 has exactly four components, so negative or >= 4 indices are invalid.","triggerScenarios":"Reading color4[index] with index < 0 or index > 3, e.g. a loop bound copied from a larger vector type or a computed index going negative.","commonSituations":"Generic per-channel loops with wrong upper bounds; math utilities shared with Vector4-like types where a fifth slot was expected; off-by-one in alpha handling.","solutions":["Clamp or check the index to 0..3 before access.","Iterate with an explicit bound of 4 for Color4.","Access R/G/B/A fields directly for known components."],"exampleFix":"// before\nfor (int i = 0; i <= 4; i++)\n    sum += color4[i];\n// after\nfor (int i = 0; i < 4; i++)\n    sum += color4[i];","handlingStrategy":"validation","validationCode":"if (index < 0 || index > 3) throw new ArgumentOutOfRangeException(nameof(index));\nvar channel = color4[index];","typeGuard":"bool IsValidColor4Index(int i) => i is >= 0 and <= 3;","tryCatchPattern":"try { var c = color4[index]; }\ncatch (ArgumentOutOfRangeException) { /* clamp index and retry or default */ }","preventionTips":["Use < 4 (not <= 4) as loop bound for Color4 channels.","Define channel-count constants alongside each color type.","Prefer named field access to avoid index math entirely."],"tags":["argument-out-of-range","indexing","csharp"],"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"}