{"record":{"id":"89c640f77dd43c65","repo":"stride3d/stride","slug":"arrayindex-is-greater-than-or-equal-to-array-length","errorCode":null,"errorMessage":"arrayIndex is greater than or equal to array.Length","messagePattern":"arrayIndex is greater than or equal to array\\.Length","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":276,"sourceCode":"    {\n        defaultCapacity = INITIAL_SIZE;\n        this.table = new KeyValuePair<TKey, TValue>[defaultCapacity];\n        inUse = 0;\n        modificationCount++;\n    }\n\n    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)\n    {\n        if (Count == 0)\n            return;\n\n        ArgumentNullException.ThrowIfNull(array);\n\n        if (arrayIndex < 0)\n            throw new ArgumentOutOfRangeException();\n\n        if (arrayIndex >= array.Length)\n            throw new ArgumentNullException(\"arrayIndex is greater than or equal to array.Length\");\n        if (Count > (array.Length - arrayIndex))\n            throw new ArgumentNullException(\"Not enough space in array from arrayIndex to end of array\");\n\n        var i = arrayIndex;\n        foreach (var pair in this)\n            array[i++] = pair;\n    }\n\n    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)\n    {\n        Add(keyValuePair.Key, keyValuePair.Value);\n    }\n\n    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)\n    {\n        var i = Find(keyValuePair.Key);\n\n        if (i >= 0)","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L258-L294","documentation":"CopyTo on Stride's SortedList throws when the caller-supplied starting index (arrayIndex) is greater than or equal to the destination array's Length, meaning there is no room to write even one element. The library uses an argument-validation guard before iterating elements. Note the code mistakenly throws ArgumentNullException with a descriptive message; the type is misleading but the condition is an ArgumentOutOfRange/Index scenario.","triggerScenarios":"Calling CopyTo(array, arrayIndex) where arrayIndex >= array.Length, e.g. CopyTo(new KeyValuePair<K,V>[0], 0) or CopyTo(arr, arr.Length).","commonSituations":"Copying into a zero-length or undersized buffer, computing the offset from a wrong cursor/offset variable, or passing an array sized for a different collection.","solutions":["Ensure array.Length is greater than arrayIndex before calling CopyTo.","Size the destination array to at least sortedList.Count plus the offset: new KeyValuePair<TKey,TValue>[list.Count + arrayIndex].","If copying from index 0, just use new KeyValuePair<TKey,TValue>[list.Count]."],"exampleFix":"// before\nvar pairs = new KeyValuePair<string,int>[0];\nsortedList.CopyTo(pairs, 0);\n// after\nvar pairs = new KeyValuePair<string,int>[sortedList.Count];\nsortedList.CopyTo(pairs, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (arrayIndex < 0 || arrayIndex >= array.Length)\n    throw new ArgumentOutOfRangeException(nameof(arrayIndex));\nif (sortedList.Count > array.Length - arrayIndex)\n    throw new ArgumentException(\"Destination array too small.\");","typeGuard":"static bool CanCopyTo<T>(KeyValuePair<TKey,TValue>[] array, int index, int count) =>\n    array != null && index >= 0 && index < array.Length && count <= array.Length - index;","tryCatchPattern":"try { sortedList.CopyTo(array, arrayIndex); }\ncatch (ArgumentNullException ex) when (ex.Message.Contains(\"arrayIndex\")) { /* resize and retry */ }","preventionTips":["Always size the destination array from sortedList.Count plus the offset.","Validate arrayIndex against array.Length before any CopyTo call.","Prefer ToArray() for snapshots instead of manual buffers."],"tags":["collections","argument-validation","array-copy"],"backgroundTag":"value-out-of-range","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"}