{"record":{"id":"5f4db2463831e920","repo":"JamesNK/Newtonsoft.Json","slug":"index-is-less-than-0","errorCode":null,"errorMessage":"Index is less than 0.","messagePattern":"Index is less than 0\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":419,"sourceCode":"            }\n#endif\n#if HAVE_INOTIFY_COLLECTION_CHANGED\n            if (_collectionChanged != null)\n            {\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            {","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L401-L437","documentation":"RemoveItemAt throws ArgumentOutOfRangeException when the index passed to RemoveAt is negative. The method validates index < 0 before touching the child list.","triggerScenarios":"jArray.RemoveAt(-1); a computed index that underflows to negative; RemoveAt on an index derived from IndexOf returning -1.","commonSituations":"Using IndexOfItem result (-1 when not found) directly as a remove index; arithmetic that goes below zero.","solutions":["Guard the index: only call RemoveAt when index >= 0.","Check the result of IndexOf for -1 (not found) before removing.","Use Remove(item) which safely returns false when the item is absent."],"exampleFix":"// before\narr.RemoveAt(arr.IndexOf(item));\n// after\narr.Remove(item);","handlingStrategy":"validation","validationCode":"if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));\narr.RemoveAt(index);","typeGuard":null,"tryCatchPattern":"try { arr.RemoveAt(index); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\") {\n    // index was invalid; ignore or report\n}","preventionTips":["Guard index >= 0 before RemoveAt.","Use Remove(item) which safely returns false when absent.","Never feed IndexOf's -1 result straight into RemoveAt."],"tags":["jcontainer","remove","bounds","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}