stride3d/stride · error · ArgumentOutOfRangeException
There must be two and only two input values for Double2.
Error message
There must be two and only two input values for Double2.
What it means
The Double2(double[] values) constructor requires the input array to contain exactly two elements (X and Y). The library throws ArgumentOutOfRangeException when the array length differs from 2, after a separate null check.
Solutions
- Validate values.Length == 2 before constructing; slice or pad the array as needed.
- If you have more data, extract exactly two elements: new Double2(data[0], data[1]).
- Use the Double2(x, y) constructor instead of the array constructor when components are known individually.
Example fix
// before
var v = new Double2(tokens);
// after
if (tokens.Length != 2) throw new FormatException("Expected 2 components");
var v = new Double2(tokens[0], tokens[1]); Defensive patterns
Strategy: validation
Validate before calling
if (values is null) throw new ArgumentNullException(nameof(values));
if (values.Length != 2) throw new ArgumentException("Double2 requires exactly 2 values", nameof(values));
var v = new Double2(values); Type guard
bool IsPair(double[]? a) => a is { Length: 2 }; Try / catch
try { var v = new Double2(values); }
catch (ArgumentOutOfRangeException ex) { /* handle malformed input array */ } Prevention
- Slice or validate input arrays to exactly 2 elements before constructing.
- Prefer the Double2(x, y) constructor when components are known.
- Validate parsed/config data lengths at the parsing boundary.
When it happens
Trigger: Calling new Double2(someArray) where someArray.Length != 2, e.g. passing an empty array, a single-element array, or a larger buffer.
Common situations: Building vectors from parsed config/file data where the split produced fewer or more tokens; passing a raw data buffer instead of a 2-element slice; upstream data schema changes altering array length.
Related errors
- There must be four and only four input values for Double4.
- There must be three and only three input values for Color3.
- There must be 3 or 4 float[] values for Color4.
- There must be four and only four input values for ColorBGRA.
- Indices for Double2 run from 0 to 1, inclusive.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/48f032357970eb45.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/Double2.cs:86
/// <param name="x">Initial value for the X component of the vector.</param>
/// <param name="y">Initial value for the Y component of the vector.</param>
public Double2(double x, double y)
{
X = x;
Y = y;
}
/// <summary>
/// Initializes a new instance of the <see cref="Double2"/> struct.
/// </summary>
/// <param name="values">The values to assign to the X and Y components of the vector. This must be an array with two 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 two elements.</exception>
public Double2(double[] values)
{
ArgumentNullException.ThrowIfNull(values);
if (values.Length != 2)
throw new ArgumentOutOfRangeException(nameof(values), "There must be two and only two input values for Double2.");
X = values[0];
Y = values[1];
}
/// <summary>
/// Initializes a new instance of the <see cref="Double2"/> struct.
/// </summary>
/// <param name="v">The Vector2 to construct the Double2 from.</param>
public Double2(Vector2 v)
{
X = v.X;
Y = v.Y;
}
/// <summary>
/// Gets a value indicting whether this instance is normalized.
/// </summary>View on GitHub (pinned to 96fad776d2)