{"record":{"id":"561455382f6e251d","repo":"stride3d/stride","slug":"the-index-must-be-an-int","errorCode":null,"errorMessage":"The index must be an int.","messagePattern":"The index must be an int\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Reflection/TypeDescriptors/ListDescriptor.cs","lineNumber":113,"sourceCode":"    /// </summary>\n    /// <param name=\"list\">The list.</param>\n    /// <returns>A generic enumerator.</returns>\n    /// <exception cref=\"System.ArgumentNullException\">dictionary</exception>\n    public IEnumerable<object> GetEnumerator(object list)\n    {\n        ArgumentNullException.ThrowIfNull(list);\n        return ((IEnumerable)list).Cast<object>();\n    }\n\n    /// <summary>\n    /// Returns the value matching the given index in the list.\n    /// </summary>\n    /// <param name=\"list\">The list.</param>\n    /// <param name=\"index\">The index.</param>\n    public override object? GetValue(object list, object index)\n    {\n        ArgumentNullException.ThrowIfNull(list);\n        if (index is not int) throw new ArgumentException(\"The index must be an int.\");\n        return GetValue(list, (int)index);\n    }\n\n    /// <summary>\n    /// Returns the value matching the given index in the list.\n    /// </summary>\n    /// <param name=\"list\">The list.</param>\n    /// <param name=\"index\">The index.</param>\n    public override object? GetValue(object list, int index)\n    {\n        ArgumentNullException.ThrowIfNull(list);\n        return getIndexedItemMethod(list, index);\n    }\n\n    public override void SetValue(object list, object index, object? value)\n    {\n        ArgumentNullException.ThrowIfNull(list);\n        if (index is not int) throw new ArgumentException(\"The index must be an int.\");","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Reflection/TypeDescriptors/ListDescriptor.cs#L95-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Cast or convert the index to int before calling GetValue (e.g. (int)myLong or Convert.ToInt32(index)).","If the index is a string, parse it first with int.Parse/int.TryParse.","If you genuinely have 64-bit indices, verify the list length fits in int, then narrow to int."],"exampleFix":"// before\nobject idx = 5L;\nvar value = descriptor.GetValue(list, idx);\n// after\nobject idx = 5L;\nvar value = descriptor.GetValue(list, (int)(long)idx);","handlingStrategy":"type-guard","validationCode":"if (index is int i)\n    descriptor.GetValue(list, i);\nelse if (index is long l)\n    descriptor.GetValue(list, checked((int)l));\nelse\n    throw new ArgumentOutOfRangeException(nameof(index), index?.GetType().Name, \"Index must be int\");","typeGuard":"static bool IsValidListIndex(object? index) => index is int;","tryCatchPattern":"try { var v = descriptor.GetValue(list, index); }\ncatch (ArgumentException ex) when (ex.Message == \"The index must be an int.\") { /* convert index to int and retry */ }","preventionTips":["Always use int counters when enumerating lists via descriptors.","Never pass object-typed indices without confirming their runtime type is int.","Convert long/string indices at the API boundary with checked casts or int.TryParse."],"tags":["reflection","argument","type-mismatch","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}