stride3d/stride · error · ArgumentException

The index must be an int.

Error message

The index must be an int.

What it means

ListDescriptor<T>.GetValue(object list, object index) is the boxed-index overload used by generic reflection code. The library only supports int-based list indexing, so before unboxing it checks `index is not int` and throws ArgumentException if the caller passed anything else (long, string, etc.). This keeps the descriptor's contract identical to IList<T>'s int indexer.

Solutions

  1. Cast or convert the index to int before calling GetValue (e.g. (int)myLong or Convert.ToInt32(index)).
  2. If the index is a string, parse it first with int.Parse/int.TryParse.
  3. If you genuinely have 64-bit indices, verify the list length fits in int, then narrow to int.

Example fix

// before
object idx = 5L;
var value = descriptor.GetValue(list, idx);
// after
object idx = 5L;
var value = descriptor.GetValue(list, (int)(long)idx);
Defensive patterns

Strategy: type-guard

Validate before calling

if (index is int i)
    descriptor.GetValue(list, i);
else if (index is long l)
    descriptor.GetValue(list, checked((int)l));
else
    throw new ArgumentOutOfRangeException(nameof(index), index?.GetType().Name, "Index must be int");

Type guard

static bool IsValidListIndex(object? index) => index is int;

Try / catch

try { var v = descriptor.GetValue(list, index); }
catch (ArgumentException ex) when (ex.Message == "The index must be an int.") { /* convert index to int and retry */ }

Prevention

When it happens

Trigger: Calling ListDescriptor.GetValue(list, index) with a non-int boxed index, e.g. a long, short, or string index. Typically happens in generic/serialization code that treats indices as object and assumes numeric widening that C# does not do for unboxing.

Common situations: Serializers or editors iterating collections with a long counter; script/property-grid code passing user-entered index strings; ported code from VB/Java where integral types auto-convert.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Reflection/TypeDescriptors/ListDescriptor.cs:113

    /// </summary>
    /// <param name="list">The list.</param>
    /// <returns>A generic enumerator.</returns>
    /// <exception cref="System.ArgumentNullException">dictionary</exception>
    public IEnumerable<object> GetEnumerator(object list)
    {
        ArgumentNullException.ThrowIfNull(list);
        return ((IEnumerable)list).Cast<object>();
    }

    /// <summary>
    /// Returns the value matching the given index in the list.
    /// </summary>
    /// <param name="list">The list.</param>
    /// <param name="index">The index.</param>
    public override object? GetValue(object list, object index)
    {
        ArgumentNullException.ThrowIfNull(list);
        if (index is not int) throw new ArgumentException("The index must be an int.");
        return GetValue(list, (int)index);
    }

    /// <summary>
    /// Returns the value matching the given index in the list.
    /// </summary>
    /// <param name="list">The list.</param>
    /// <param name="index">The index.</param>
    public override object? GetValue(object list, int index)
    {
        ArgumentNullException.ThrowIfNull(list);
        return getIndexedItemMethod(list, index);
    }

    public override void SetValue(object list, object index, object? value)
    {
        ArgumentNullException.ThrowIfNull(list);
        if (index is not int) throw new ArgumentException("The index must be an int.");

View on GitHub (pinned to 96fad776d2)