stride3d/stride · error · ArgumentOutOfRangeException

There must be two and only two input values for Half2.

Error message

There must be two and only two input values for Half2.

What it means

Thrown by the Half2(Half[]) constructor when the input array does not contain exactly two elements. Half2 is a fixed-size 2-component vector (X, Y), so it can only be built from exactly two Half values. Thrown as ArgumentOutOfRangeException for the 'values' parameter.

Solutions

  1. Ensure the array has exactly 2 elements before constructing
  2. If data is 3D, use Half3 instead of Half2
  3. Slice the array to exactly two elements when extracting from a larger buffer

Example fix

// before
var uv = new Half2(halfs); // halfs.Length == 3 -> throws
// after
var uv = new Half2(new[] { halfs[0], halfs[1] });
Defensive patterns

Strategy: validation

Validate before calling

if (values is null) throw new ArgumentNullException(nameof(values));
if (values.Length != 2) throw new ArgumentException($"Half2 requires exactly 2 values, got {values.Length}.", nameof(values));

Type guard

static bool IsValidHalf2Source(Half[]? values) => values is { Length: 2 };

Try / catch

try { var v = new Half2(values); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "values")
{
    // handle malformed packed data
    var v = new Half2(Half.Zero, Half.Zero);
}

Prevention

When it happens

Trigger: Calling new Half2(Half[]) with an array of length 0, 1, or 3+ — e.g. passing a Half3's raw component array or an empty placeholder array.

Common situations: Unpacking packed half-float buffers where the stride/offset math is off by one; passing half-precision UV data that includes an extra padding element; converting from float arrays without converting length.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Half2.cs:106

    /// </summary>
    /// <param name="value">The value to set for both the X and Y components.</param>
    public Half2(Half value)
    {
        X = value;
        Y = value;
    }

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

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

    /// <summary>
    /// Initializes a new instance of the <see cref="Half2"/> structure.
    /// </summary>
    /// <param name="x">The X component.</param>
    /// <param name="y">The Y component.</param>
    public Half2(float x, float y)
    {
        X = (Half)x;
        Y = (Half)y;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Half2"/> structure.

View on GitHub (pinned to 96fad776d2)