stride3d/stride · error · ArgumentOutOfRangeException

There must be three and only three input values for Int3.

Error message

There must be three and only three input values for Int3.

What it means

The Int3(values) array constructor requires the input array to contain exactly three elements and throws when the length differs. It is a length guard on the constructor input: an array with fewer than three elements leaves X/Y/Z undefined, and a longer one is a caller mistake, so both fire the error.

Solutions

  1. Validate values.Length == 3 before calling the constructor
  2. Fix the parsing/splitting code that produced the wrong-size array
  3. If the source has 4 components, slice explicitly: new Int3(arr[0], arr[1], arr[2])

Example fix

// before
var v = new Int3(File.ReadAllLines(...)[0].Split(',').Select(int.Parse).ToArray()); // may be wrong length
// after
var vals = line.Split(',').Select(int.Parse).ToArray();
if (vals.Length != 3) throw new FormatException($"Expected 3 values, got {vals.Length}");
var v = new Int3(vals);
Defensive patterns

Strategy: validation

Validate before calling

if (values is null || values.Length != 3)
    throw new ArgumentException("Int3 requires exactly 3 elements", nameof(values));
var v = new Int3(values);

Type guard

static bool IsValidInt3Source(int[]? values) => values is { Length: 3 };

Try / catch

try { var v = new Int3(values); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "values")
{
    // log values.Length and handle malformed input
}

Prevention

When it happens

Trigger: Calling new Int3(int[]) with an array of length 0, 1, 2, 4, etc. — e.g. splitting a line that produced an extra token, or passing a 4-element RGBA/color array to construct a position.

Common situations: Parsing CSV/obj/config data with inconsistent tuple sizes; mixing 3D position (3) and quaternion/color (4) arrays; trailing empty string from Split making 4 tokens.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/1ddd33a979800e7e. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Int3.cs:138

    /// <param name="z">Initial value for the Z component of the vector.</param>
    public Int3(Vector2 value, int z)
    {
        X = (int)value.X;
        Y = (int)value.Y;
        Z = z;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Int3"/> 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 Int3(int[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 3)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be three and only three input values for Int3.");

        X = values[0];
        Y = values[1];
        Z = values[2];
    }

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the X, Y, or Z component, depending on the index.</value>
    /// <param name="index">The index of the component to access. Use 0 for the X component, 1 for the Y component, and 2 for the Z component.</param>
    /// <returns>The value of the component at the specified index.</returns>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 2].</exception>
    public int this[int index]
    {
        readonly get
        {
            return index switch

View on GitHub (pinned to 96fad776d2)