{"record":{"id":"4d2a7ba13572efb5","repo":"BornToBeRoot/NETworkManager","slug":"length-must-be-greater-than-zero","errorCode":null,"errorMessage":"Length must be greater than zero.","messagePattern":"Length must be greater than zero\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/NETworkManager.Utilities/ListHelper.cs","lineNumber":29,"sourceCode":"    /// Modify a list by adding the <paramref name=\"entry\"/> and removing the oldest entry if the list is full.\n    /// If an entry or multiple ones already exist in the list, they will be removed before adding the new entry.\n    /// </summary>\n    /// <param name=\"list\">List to modify. Used with <paramref name=\"entry\"/> to add and remove entries.</param>\n    /// <param name=\"entry\">Entry to add to the list.</param>\n    /// <param name=\"length\">Maximum length of the list. Oldest entries will be removed if the list exceeds this length.</param>\n    /// <typeparam name=\"T\">Type of the list entries. Currently <see cref=\"string\"/> or <see cref=\"int\"/>.</typeparam>\n    /// <returns>Modified list with the new entry added and oldest entries removed if necessary.</returns>\n    public static List<T> Modify<T>(List<T> list, T entry, int length)\n    {\n        int index;\n\n        while ((index = list.IndexOf(entry)) != -1)\n        {\n            list.RemoveAt(index);\n        }\n\n        if (length <= 0)\n            throw new ArgumentOutOfRangeException(nameof(length), \"Length must be greater than zero.\");\n\n        while (list.Count >= length)\n            list.RemoveAt(list.Count - 1);\n\n        list.Insert(0, entry);\n\n        return list;\n    }\n}\n","sourceCodeStart":11,"sourceCodeEnd":39,"githubUrl":"https://github.com/BornToBeRoot/NETworkManager/blob/2780d65469917a296dbc15f206107d72e2d0115c/Source/NETworkManager.Utilities/ListHelper.cs#L11-L39","documentation":"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)).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a positive length (>= 1) to Modify","Validate/clamp the configured max-history value before calling: length = Math.Max(1, configuredLength)","Fix the settings source so the history-size setting is a positive integer","Catch ArgumentOutOfRangeException and retry with a sane default like 10"],"exampleFix":"// before\nListHelper.Modify(list, entry, maxLengthFromSettings); // throws when 0\n// after\nvar length = Math.Max(1, maxLengthFromSettings);\nListHelper.Modify(list, entry, length);","handlingStrategy":"validation","validationCode":"if (length <= 0)\n    throw new ArgumentException(\"length must be positive\", nameof(length));","typeGuard":"bool IsValidHistoryLength(int length) => length > 0;","tryCatchPattern":"try { ListHelper.Modify(list, entry, length); }\ncatch (ArgumentOutOfRangeException) { ListHelper.Modify(list, entry, Math.Max(1, length)); }","preventionTips":["Clamp configured history sizes with Math.Max(1, value)","Validate settings on load so history-size cannot be 0/negative","Guard parsed settings values with TryParse plus range checks","Document that length must be positive"],"tags":["argument","range","history"],"backgroundTag":"argument-out-of-range","analyzedSha":"2780d65469917a296dbc15f206107d72e2d0115c","analyzedAt":"2026-09-12T17:00:17.986Z","contentChangedAt":"2026-09-12T17:00:17.986Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}