stride3d/stride · error · ArgumentOutOfRangeException
There must be four and only four input values for UInt4.
Error message
There must be four and only four input values for UInt4.
What it means
The UInt4(uint[] values) constructor requires the input array to contain exactly four elements, one for each component X, Y, Z, W. If the array length differs from 4 (and is not null), ArgumentOutOfRangeException is thrown with this message.
Solutions
- Ensure the array has exactly 4 elements before constructing: pad with 0 or truncate as appropriate.
- Check the data source's component count (3 vs 4 channels) and adapt it to four.
- Use a fixed-size alternative or explicit constructor UInt4(x, y, z, w) when element count is known at compile time.
Example fix
// before
var v = new UInt4(rgbaBuffer); // rgbaBuffer.Length may be 3
// after
if (rgbaBuffer.Length != 4)
Array.Resize(ref rgbaBuffer, 4);
var v = new UInt4(rgbaBuffer); Defensive patterns
Strategy: validation
Validate before calling
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 4)
throw new ArgumentException($"UInt4 requires exactly 4 values, got {values.Length}.", nameof(values));
var v = new UInt4(values); Try / catch
try
{
var v = new UInt4(values);
}
catch (ArgumentOutOfRangeException ex)
{
// wrong element count — inspect ex.ParamName/values.Length and fix the data source
logger.LogError(ex, "UInt4 construction failed with {Count} values", values?.Length);
} Prevention
- Prefer the explicit UInt4(x, y, z, w) constructor over array input.
- Assert array length 4 at deserialization boundaries.
- Keep component counts consistent between data formats (RGB vs RGBA) and vector types.
When it happens
Trigger: Calling new UInt4(array) with an array whose Length is not 4, e.g. a 3-element array from deserialized data or a padded 5-element buffer.
Common situations: Deserializing packed data that lost a component; passing a Vector3/Color components array (3 elements) instead of 4; building the array with a loop that miscounts; upstream format change from RGB to RGBA or vice versa.
Related errors
- The destination array must be of same length or larger…
- There must be two and only two input values for Vector2.
- Indices for ColorBGRA run from 0 to 3, inclusive.
- Indices for Int2 run from 0 to 1, inclusive.
- Indices for Int3 run from 0 to 2, inclusive.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/fafda10a3f787242.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/UInt4.cs:130
{
X = x;
Y = y;
Z = z;
W = w;
}
/// <summary>
/// Initializes a new instance of the <see cref = "UInt4" /> struct.
/// </summary>
/// <param name = "values">The values to assign to the X, Y, Z, and W components of the vector. 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 UInt4(uint[] 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 UInt4.");
X = values[0];
Y = values[1];
Z = values[2];
W = values[3];
}
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the X, Y, Z, or W 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, 2 for the Z component, and 3 for the W component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref = "System.ArgumentOutOfRangeException">Thrown when the <paramref name = "index" /> is out of the range [0, 3].</exception>
public uint this[uint index]
{
get
{View on GitHub (pinned to 96fad776d2)