stride3d/stride · error · ArgumentOutOfRangeException
There must be two and only two input values for Int2.
Error message
There must be two and only two input values for Int2.
What it means
Int2's array constructor requires the input array to contain exactly two elements, one for X and one for Y. The library throws ArgumentOutOfRangeException when values.Length != 2 so that partially-filled components are never silently zeroed. It is thrown after a null check, so the array was non-null but the wrong size.
Solutions
- Ensure the array passed to new Int2(...) has exactly 2 elements before constructing
- Check the source of the array (split/parsing code) for stray empty or extra tokens
- If the data may have 3+ components (e.g. RGB), explicitly slice: new Int2(arr[0], arr[1]) or arr.Take(2).ToArray()
Example fix
// before
var parts = line.Split(',');
var v = new Int2(parts.Select(int.Parse).ToArray()); // crashes if 3 tokens
// after
var parts = line.Split(',');
if (parts.Length != 2) throw new FormatException($"Expected 2 values, got {parts.Length}");
var v = new Int2(parts.Select(int.Parse).ToArray()); Defensive patterns
Strategy: validation
Validate before calling
if (values is null || values.Length != 2)
throw new ArgumentException("Int2 requires exactly 2 elements", nameof(values));
var v = new Int2(values); Type guard
static bool IsValidInt2Source(int[]? values) => values is { Length: 2 }; Try / catch
try { var v = new Int2(values); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "values")
{
// log values.Length and fall back / report bad data
} Prevention
- Always check array length before constructing vectors from arrays
- Use constructor overloads new Int2(x, y) when components come from separate variables
- Guard Split() parsing against trailing separators producing empty tokens
When it happens
Trigger: Calling new Int2(int[]) with an array of length 0, 1, 3, or more — e.g. parsing a config string with Split(',') that yielded an extra token, or passing a 3/4-element color or vector array where a 2-element one was expected.
Common situations: Deserializing data files whose vector entries have inconsistent element counts; mixing up Int2 with Int3/Int4 arrays; off-by-one splits producing a trailing empty token.
Related errors
- Indices for Int2 run from 0 to 1, inclusive.
- There must be three and only three input values for Int3.
- Indices for Int3 run from 0 to 2, inclusive.
- Indices for Int4 run from 0 to 3, inclusive.
- Indices for Matrix run from 0 to 15, inclusive.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/1aad20c32b839811.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/Int2.cs:122
/// </summary>
/// <param name="value">A vector containing the values with which to initialize the X and Y components.</param>
public Int2(Vector2 value)
{
X = (int)value.X;
Y = (int)value.Y;
}
/// <summary>
/// Initializes a new instance of the <see cref="Int2"/> struct.
/// </summary>
/// <param name="values">The values to assign to the X, Y, and Z components of the vector. This must be an array with three elements.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than three elements.</exception>
public Int2(int[] values)
{
ArgumentNullException.ThrowIfNull(values);
if (values.Length != 2)
throw new ArgumentOutOfRangeException(nameof(values), "There must be two and only two input values for Int2.");
X = values[0];
Y = values[1];
}
/// <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 int this[int index]
{
readonly get
{
return index switch
{View on GitHub (pinned to 96fad776d2)