BornToBeRoot/NETworkManager · error · ArgumentOutOfRangeException

Length must be greater than zero.

Error message

Length must be greater than zero.

What it means

ListHelper.Modify deduplicates an entry in a list, trims the list to a maximum length, and inserts the entry at position 0 (a fixed-size history pattern). If the requested length is <= 0 no valid history can be kept, so it throws ArgumentOutOfRangeException(nameof(length)).

Solutions

  1. Pass a positive length (>= 1) to Modify
  2. Validate/clamp the configured max-history value before calling: length = Math.Max(1, configuredLength)
  3. Fix the settings source so the history-size setting is a positive integer
  4. Catch ArgumentOutOfRangeException and retry with a sane default like 10

Example fix

// before
ListHelper.Modify(list, entry, maxLengthFromSettings); // throws when 0
// after
var length = Math.Max(1, maxLengthFromSettings);
ListHelper.Modify(list, entry, length);
Defensive patterns

Strategy: validation

Validate before calling

if (length <= 0)
    throw new ArgumentException("length must be positive", nameof(length));

Type guard

bool IsValidHistoryLength(int length) => length > 0;

Try / catch

try { ListHelper.Modify(list, entry, length); }
catch (ArgumentOutOfRangeException) { ListHelper.Modify(list, entry, Math.Max(1, length)); }

Prevention

When it happens

Trigger: Calling Modify with a length parameter of 0 or a negative number — e.g. a max-history setting read as 0 from a corrupt/default settings file, or a caller passing an uninitialized size variable.

Common situations: Settings migration where max history entries defaulted to 0; user entering 0 or negative value in a 'number of recent items' setting; integer parse failure yielding 0.


AI-assisted analysis of BornToBeRoot/NETworkManager@2780d65469 (2026-09-12). Data as JSON: /api/errors/4d2a7ba13572efb5. Report an issue: GitHub.

Appendix: source

Thrown at Source/NETworkManager.Utilities/ListHelper.cs:29

    /// Modify a list by adding the <paramref name="entry"/> and removing the oldest entry if the list is full.
    /// If an entry or multiple ones already exist in the list, they will be removed before adding the new entry.
    /// </summary>
    /// <param name="list">List to modify. Used with <paramref name="entry"/> to add and remove entries.</param>
    /// <param name="entry">Entry to add to the list.</param>
    /// <param name="length">Maximum length of the list. Oldest entries will be removed if the list exceeds this length.</param>
    /// <typeparam name="T">Type of the list entries. Currently <see cref="string"/> or <see cref="int"/>.</typeparam>
    /// <returns>Modified list with the new entry added and oldest entries removed if necessary.</returns>
    public static List<T> Modify<T>(List<T> list, T entry, int length)
    {
        int index;

        while ((index = list.IndexOf(entry)) != -1)
        {
            list.RemoveAt(index);
        }

        if (length <= 0)
            throw new ArgumentOutOfRangeException(nameof(length), "Length must be greater than zero.");

        while (list.Count >= length)
            list.RemoveAt(list.Count - 1);

        list.Insert(0, entry);

        return list;
    }
}

View on GitHub (pinned to 2780d65469)