stride3d/stride · error · ArgumentOutOfRangeException

There must be four and only four input values for…

Error message

There must be four and only four input values for Quaternion.

What it means

Quaternion's float[] constructor requires exactly 4 elements (X, Y, Z, W). An array of any other length triggers ArgumentOutOfRangeException because a quaternion is defined by precisely these four components.

Solutions

  1. Pass an array with exactly 4 floats (X, Y, Z, W)
  2. Check values.Length == 4 before construction and handle otherwise
  3. Use the Quaternion(x, y, z, w) constructor to avoid array length ambiguity

Example fix

// before
var q = new Quaternion(vec3Array); // float[3]
// after
var q = new Quaternion(x, y, z, w);
Defensive patterns

Strategy: validation

Validate before calling

if (values == null || values.Length != 4) throw new ArgumentException("Quaternion requires exactly 4 floats: X, Y, Z, W");
var q = new Quaternion(values);

Type guard

bool IsValidQuaternionArray(float[] v) => v != null && v.Length == 4;

Try / catch

try { var q = new Quaternion(values); }
catch (ArgumentOutOfRangeException ex) { /* ex.ParamName == "values"; handle wrong-length array */ }

Prevention

When it happens

Trigger: Calling new Quaternion(float[] values) with an array whose Length is not 4, e.g. a 3-element vector array or a rotation matrix row.

Common situations: Reusing vector (x,y,z) arrays for quaternions; parsing rotation data that includes an extra angle; truncated deserialization of quaternion payloads.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Quaternion.cs:162

    {
        X = x;
        Y = y;
        Z = z;
        W = w;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Stride.Core.Mathematics.Quaternion"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the X, Y, Z, and W components of the quaternion. This must be an array with four 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 four elements.</exception>
    public Quaternion(float[] values)
    {
        if (values == null)
            throw new ArgumentNullException(nameof(values));
        if (values.Length != 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Quaternion.");

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

    /// <summary>
    /// Gets a value indicating whether this instance is equivalent to the identity quaternion.
    /// </summary>
    /// <value>
    /// <c>true</c> if this instance is an identity quaternion; otherwise, <c>false</c>.
    /// </value>
    public bool IsIdentity
    {
        get { return this.Equals(Identity); }
    }

View on GitHub (pinned to 96fad776d2)