stride3d/stride · error · ArgumentOutOfRangeException

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

Error message

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

What it means

The Double3(double[] values) constructor requires exactly three elements and copies them into X, Y and Z. A null array throws ArgumentNullException; an array of any other length throws ArgumentOutOfRangeException. This guard guarantees the component fields are always initialized from a complete triple.

Solutions

  1. Ensure the array has exactly 3 elements before constructing, e.g. if (arr.Length != 3) handle it
  2. Slice or pad the data to three values first (arr.Take(3).ToArray() or extend a 2-element array)
  3. For scattered data, build the vector explicitly: new Double3(arr[0], arr[1], arr[2]) after bounds checks

Example fix

// before
var v = new Double3(tokens);
// after
if (tokens.Length == 3)
    var v = new Double3(tokens);
else
    throw new FormatException($"Expected 3 components, got {tokens.Length}");
Defensive patterns

Strategy: validation

Validate before calling

if (values == null) throw new ArgumentNullException(nameof(values));
if (values.Length != 3) throw new FormatException($"Double3 needs 3 components, got {values.Length}");
var v = new Double3(values);

Type guard

static bool IsDouble3Array(double[]? a) => a is { Length: 3 };

Try / catch

try { var v = new Double3(values); }
catch (ArgumentOutOfRangeException) { /* handle malformed input: log, use default, or rethrow as data error */ }

Prevention

When it happens

Trigger: Calling new Double3(double[]) with an array whose Length is not 3 (0, 1, 2, 4+ elements).

Common situations: Deserializing a vector from parsed text/CSV/config where the split produced an unexpected number of tokens; passing a byte or float buffer before slicing it to three values.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Double3.cs:112

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

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

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

    /// <summary>
    /// Initializes a new instance of the <see cref="Double3"/> struct.
    /// </summary>
    /// <param name="v">The Vector3 to construct the Double3 from.</param>
    public Double3(Vector3 v)
    {
        X = v.X;
        Y = v.Y;
        Z = v.Z;
    }

    /// <summary>

View on GitHub (pinned to 96fad776d2)