stride3d/stride · error · ArgumentException

ArgumentException

Error message

ArgumentException

What it means

Capacity contract guard in SortedDictionary.KeyCollection.CopyTo: the destination array is too small — array.Length minus index is less than the key count — so the keys cannot all be copied into it. The faulting input is the array (or the index) passed to CopyTo.

Solutions

  1. Size the array to Count + index before copying
  2. Re-allocate the buffer each time instead of caching it
  3. Check array.Length - index >= collection.Count before calling CopyTo

Example fix

// before
var arr = new TKey[keys.Count]; // stale, dict grew
keys.CopyTo(arr, 1);
// after
var arr = new TKey[keys.Count + 1];
keys.CopyTo(arr, 1);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Length - index < keyCollection.Count) array = new TKey[keyCollection.Count + index];

Type guard

null

Try / catch

try { keys.CopyTo(arr, index); } catch (ArgumentException) { arr = new TKey[keys.Count + index]; keys.CopyTo(arr, index); }

Prevention

When it happens

Trigger: Calling CopyTo with an array smaller than Count minus the starting index, e.g. reusing a stale array after the dictionary grew.

Common situations: Caching a buffer sized from an earlier Count, then the dictionary gained entries; off-by-one when reserving space for the index offset.

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/97237cac11eeebef. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/SortedDictionary.cs:544

            {
                return new Enumerator(dictionary);
            }

            public void CopyTo(TKey[] array, int index)
            {
                if (array == null)
                {
                    throw new ArgumentNullException();
                }

                if (index < 0)
                {
                    throw new ArgumentOutOfRangeException();
                }

                if (array.Length - index < Count)
                {
                    throw new ArgumentException();
                }

                dictionary._set.InOrderTreeWalk(delegate(TreeSet<KeyValuePair<TKey, TValue>>.Node node)
                {
                    array[index++] = node.Item.Key;
                    return true;
                });
            }

            void ICollection.CopyTo(Array array, int index)
            {
                if (array == null)
                {
                    throw new ArgumentNullException();
                }

                if (array.Rank != 1)
                {

View on GitHub (pinned to 96fad776d2)