{"record":{"id":"baffaa4b71a67e13","repo":"JamesNK/Newtonsoft.Json","slug":"arrayindex-is-equal-to-or-greater-than-the-length-baffaa","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/JObject.cs","lineNumber":679,"sourceCode":"                return false;\n            }\n\n            return (property.Value == item.Value);\n        }\n\n        void ICollection<KeyValuePair<string, JToken?>>.CopyTo(KeyValuePair<string, JToken?>[] 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 (JProperty property in _properties)\n            {\n                array[arrayIndex + index] = new KeyValuePair<string, JToken?>(property.Name, property.Value);\n                index++;\n            }\n        }\n\n        bool ICollection<KeyValuePair<string, JToken?>>.IsReadOnly => false;\n\n        bool ICollection<KeyValuePair<string, JToken?>>.Remove(KeyValuePair<string, JToken?> item)\n        {","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L661-L697","documentation":"ArgumentException thrown by JObject's ICollection<KeyValuePair<string,JToken?>>.CopyTo when arrayIndex is >= the destination array length (and not 0). The starting position must fall within the array; an out-of-range start is rejected.","triggerScenarios":"Calling CopyTo(smallArray, smallArray.Length) or CopyTo(arr, arr.Length+1) — i.e. a start index at/past the end of the destination array. Common with empty arrays (arrayIndex 0 is allowed for the empty-array edge case, but any positive index into a zero-length array throws).","commonSituations":"Passing array.Length as the start index; an arrayIndex derived from a position counter that has advanced past the end; reusing a buffer across multiple copies without resetting the index.","solutions":["Ensure 0 <= arrayIndex < array.Length before calling (arrayIndex 0 is permitted even when the array is empty).","Allocate a destination large enough and start at 0, or track the running index and assert it stays in range.","Switch to LINQ ToArray() if buffer arithmetic is error-prone."],"exampleFix":"// before\n((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, arr.Length); // out of range\n\n// after\nif (arrayIndex < 0 || arrayIndex > arr.Length) throw new ArgumentException(\"Bad index\");\n((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, arrayIndex);","handlingStrategy":"validation","validationCode":"if (arrayIndex > array.Length) throw new ArgumentException(\"arrayIndex beyond array length.\");\n((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(array, arrayIndex);","typeGuard":null,"tryCatchPattern":"try { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, idx); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"greater than the length\")) { /* reset idx */ }","preventionTips":["Ensure 0 <= arrayIndex < array.Length (0 allowed even when array is empty).","Avoid passing array.Length as the start index.","Track running offsets carefully across chained copies."],"tags":["newtonsoft-json","linq-to-json","jobject","copyto","argument-validation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}