stride3d/stride · error · ArgumentOutOfRangeException
Indices for Vector2 run from 0 to 1, inclusive.
Error message
Indices for Vector2 run from 0 to 1, inclusive.
What it means
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.
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.
Example fix
// before for (int i = 0; i < 3; i++) sum += v[i]; // Vector2 has only 2 // after for (int i = 0; i < 2; i++) sum += v[i];
Defensive patterns
Strategy: validation
Validate before calling
if (index < 0 || index > 1)
throw new ArgumentOutOfRangeException(nameof(index), "Vector2 index must be 0 or 1.");
var component = v[index]; Type guard
bool IsValidVector2Index(int i) => i == 0 || i == 1;
Try / catch
try
{
component = v[index];
}
catch (ArgumentOutOfRangeException)
{
component = 0f;
} Prevention
- 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.
When it happens
Trigger: Reading v[i] with i >= 2 or i < 0, e.g. a loop written for Vector3/Vector4 iterating 3-4 components of a Vector2.
Common situations: Generic shader-like component loops reused across vector types; computed indices from bad data; z-vector/z-buffer code copied from 3D examples.
Related errors
- Indices for Double4 run from 0 to 3, inclusive.
- Indices for UInt4 run from 0 to 3, inclusive.
- Indices for ColorBGRA run from 0 to 3, inclusive.
- Indices for Double3 run from 0 to 2, inclusive.
- Indices for Int2 run from 0 to 1, inclusive.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/f3ba8f8e7a98f839.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/Vector2.cs:144
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the X or Y component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the X component and 1 for the Y component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 1].</exception>
public float this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
}
throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector2 run from 0 to 1, inclusive.");
}
set
{
switch (index)
{
case 0: X = value; break;
case 1: Y = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector2 run from 0 to 1, inclusive.");
}
}
}
/// <summary>
/// Casts from System.Numerics to Stride.Maths vectors
/// </summary>
/// <param name="v">Value to cast</param>
public static implicit operator Vector2(System.Numerics.Vector2 v)View on GitHub (pinned to 96fad776d2)