{"record":{"id":"31c4c3a8e653ffd6","repo":"JamesNK/Newtonsoft.Json","slug":"index-is-equal-to-or-greater-than-count","errorCode":null,"errorMessage":"Index is equal to or greater than Count.","messagePattern":"Index is equal to or greater than Count\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":423,"sourceCode":"            {\n                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));\n            }\n#endif\n\n            return true;\n        }\n\n        internal virtual void RemoveItemAt(int index)\n        {\n            IList<JToken> children = ChildrenTokens;\n\n            if (index < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), \"Index is less than 0.\");\n            }\n            if (index >= children.Count)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), \"Index is equal to or greater than Count.\");\n            }\n\n            CheckReentrancy();\n\n            JToken item = children[index];\n            JToken? previous = (index == 0) ? null : children[index - 1];\n            JToken? next = (index == children.Count - 1) ? null : children[index + 1];\n\n            if (previous != null)\n            {\n                previous.Next = next;\n            }\n            if (next != null)\n            {\n                next.Previous = previous;\n            }\n\n            item.Parent = null;","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L405-L441","documentation":"RemoveItemAt throws ArgumentOutOfRangeException when index >= ChildrenTokens.Count, i.e. the slot does not exist. The upper bound is Count-1.","triggerScenarios":"jArray.RemoveAt(jArray.Count); a stale/cached count larger than the live collection; loop variable overshooting.","commonSituations":"Iterating and removing in a forward loop without adjusting indices; cached Count after items were removed.","solutions":["Loop backwards when removing, or recompute Count each iteration.","Validate index < container.Count before calling RemoveAt.","Prefer Remove(item) when removing by reference rather than by position."],"exampleFix":"// before\nfor (int i = 0; i < arr.Count; i++) arr.RemoveAt(i);\n// after\nfor (int i = arr.Count - 1; i >= 0; i--) arr.RemoveAt(i);","handlingStrategy":"validation","validationCode":"if (index >= arr.Count || index < 0) return;\narr.RemoveAt(index);","typeGuard":null,"tryCatchPattern":"try { arr.RemoveAt(index); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\") {\n    // index out of range; recompute and retry or skip\n}","preventionTips":["Loop backwards when removing by index.","Validate index < Count immediately before RemoveAt.","Prefer Remove(item) for reference-based removal."],"tags":["jcontainer","remove","bounds","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}