stride3d/stride · error · ArgumentNullException
arrayIndex is greater than or equal to array.Length
Error message
arrayIndex is greater than or equal to array.Length
What it means
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.
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].
Example fix
// before var pairs = new KeyValuePair<string,int>[0]; sortedList.CopyTo(pairs, 0); // after var pairs = new KeyValuePair<string,int>[sortedList.Count]; sortedList.CopyTo(pairs, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0 || arrayIndex >= array.Length)
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if (sortedList.Count > array.Length - arrayIndex)
throw new ArgumentException("Destination array too small."); Type guard
static bool CanCopyTo<T>(KeyValuePair<TKey,TValue>[] array, int index, int count) =>
array != null && index >= 0 && index < array.Length && count <= array.Length - index; Try / catch
try { sortedList.CopyTo(array, arrayIndex); }
catch (ArgumentNullException ex) when (ex.Message.Contains("arrayIndex")) { /* resize and retry */ } Prevention
- 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.
When it happens
Trigger: Calling CopyTo(array, arrayIndex) where arrayIndex >= array.Length, e.g. CopyTo(new KeyValuePair<K,V>[0], 0) or CopyTo(arr, arr.Length).
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Capacity must be greater than 0.
- Capacity must be a power of two
- Not enough space in array from arrayIndex to end of array
- array is multi-dimensional
- No Collection item identifier associated to the given collec
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/89c640f77dd43c65.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core/Collections/SortedList.cs:276
{
defaultCapacity = INITIAL_SIZE;
this.table = new KeyValuePair<TKey, TValue>[defaultCapacity];
inUse = 0;
modificationCount++;
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (Count == 0)
return;
ArgumentNullException.ThrowIfNull(array);
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException();
if (arrayIndex >= array.Length)
throw new ArgumentNullException("arrayIndex is greater than or equal to array.Length");
if (Count > (array.Length - arrayIndex))
throw new ArgumentNullException("Not enough space in array from arrayIndex to end of array");
var i = arrayIndex;
foreach (var pair in this)
array[i++] = pair;
}
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)
{
Add(keyValuePair.Key, keyValuePair.Value);
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
{
var i = Find(keyValuePair.Key);
if (i >= 0)View on GitHub (pinned to 96fad776d2)