stride3d/stride · error · ArgumentOutOfRangeException

capacity too small

Error message

capacity too small

What it means

SortedList's Capacity setter throws ArgumentOutOfRangeException with message 'capacity too small' when the requested value is less than the number of elements currently in use (inUse); shrinking capacity below the current item count would lose data. The message is misleadingly non-standard for the exception type.

Solutions

  1. Clear the list (or remove enough items) before shrinking Capacity below inUse
  2. Only set Capacity >= list Count, e.g. if (newCap >= list.Count) list.Capacity = newCap;
  3. Trim capacity only when Count == 0 or use Capacity = Math.Max(desired, Count)
  4. Catch ArgumentOutOfRangeException if capacity is advisory and clamp the value instead

Example fix

// before
list.Capacity = 4; // throws if list.Count > 4
// after
if (list.Count == 0)
    list.Capacity = 4;
else
    list.Capacity = Math.Max(4, list.Count);
Defensive patterns

Strategy: validation

Validate before calling

if (desiredCapacity >= list.Count) list.Capacity = desiredCapacity;

Try / catch

try { list.Capacity = newCap; } catch (ArgumentOutOfRangeException) { list.Capacity = Math.Max(newCap, list.Count); }

Prevention

When it happens

Trigger: Setting Capacity to a value < the list's current Count while it holds items; trimming capacity aggressively after expecting an empty collection; setting Capacity = 0 while items remain in the list.

Common situations: Memory-tuning code that shrinks capacity after removals without checking Count first; config-driven capacity values smaller than loaded data; reusing a list instance that still contains items.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core/Collections/SortedList.cs:169

        {
            this[ToKey(key)] = ToValue(value);
        }
    }

    public int Capacity
    {
        get
        {
            return table.Length;
        }

        set
        {
            var current = this.table.Length;

            if (inUse > value)
            {
                throw new ArgumentOutOfRangeException("capacity too small");
            }
            if (value == 0)
            {
                // return to default size
                var newTable = new KeyValuePair<TKey, TValue>[defaultCapacity];
                Array.Copy(table, newTable, inUse);
                this.table = newTable;
            }
#if NET_1_0
            else if (current > defaultCapacity && value < current) {
                                    KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [defaultCapacity];
                                    Array.Copy (table, newTable, inUse);
                                    this.table = newTable;
                            }
#endif
            else if (value > inUse)
            {
                var newTable = new KeyValuePair<TKey, TValue>[value];

View on GitHub (pinned to 96fad776d2)