{"record":{"id":"11423924c2747e61","repo":"JamesNK/Newtonsoft.Json","slug":"arrayindex-is-equal-to-or-greater-than-the-length","errorCode":null,"errorMessage":"arrayIndex is equal to or greater than the length of array.","messagePattern":"arrayIndex is equal to or greater than the length of array\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":601,"sourceCode":"\n        internal virtual bool ContainsItem(JToken? item)\n        {\n            return (IndexOfItem(item) != -1);\n        }\n\n        internal virtual void CopyItemsTo(Array array, int arrayIndex)\n        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n            if (arrayIndex < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex), \"arrayIndex is less than 0.\");\n            }\n            if (arrayIndex >= array.Length && arrayIndex != 0)\n            {\n                throw new ArgumentException(\"arrayIndex is equal to or greater than the length of array.\");\n            }\n            if (Count > array.Length - arrayIndex)\n            {\n                throw new ArgumentException(\"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.\");\n            }\n\n            int index = 0;\n            foreach (JToken token in ChildrenTokens)\n            {\n                array.SetValue(token, arrayIndex + index);\n                index++;\n            }\n        }\n\n        internal static bool IsTokenUnchanged(JToken currentValue, JToken? newValue)\n        {\n            if (currentValue is JValue v1)\n            {","sourceCodeStart":583,"sourceCodeEnd":619,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L583-L619","documentation":"CopyItemsTo throws ArgumentException when arrayIndex is >= array.Length and is not exactly 0. The special allowance for 0 lets CopyTo succeed into a zero-length array only when no offset is requested; any other out-of-range offset is rejected.","triggerScenarios":"jArray.CopyTo(dest, dest.Length); passing an offset equal to or beyond the array length (other than 0).","commonSituations":"Destination array sized exactly without room for the offset; stale length calculation.","solutions":["Size the destination as container.Count + arrayIndex.","Ensure arrayIndex < array.Length (or use 0).","Recompute the destination length immediately before copying."],"exampleFix":"// before\narr.CopyTo(dest, dest.Length);\n// after\nvar dest = new JToken[arr.Count];\narr.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (arrayIndex != 0 && arrayIndex >= dest.Length) throw new ArgumentException(\"arrayIndex out of range.\");\ncontainer.CopyTo(dest, arrayIndex);","typeGuard":null,"tryCatchPattern":"try { container.CopyTo(dest, arrayIndex); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"length of array\")) {\n    // resize dest or reset arrayIndex to 0\n    Array.Resize(ref dest, arrayIndex + container.Count);\n    container.CopyTo(dest, arrayIndex);\n}","preventionTips":["Ensure arrayIndex < dest.Length (or use 0).","Size destination as container.Count + arrayIndex.","Recompute dest.Length immediately before copying."],"tags":["jcontainer","copyto","bounds","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}