{"record":{"id":"32a7bf2a85a2ff47","repo":"microsoft/autogen","slug":"specified-argument-was-out-of-the-range-of-valid-v","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'arrayIndex')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'arrayIndex'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/AgentChat/Abstractions/Messages.cs","lineNumber":326,"sourceCode":"    }\n\n    /// <inheritdoc cref=\"ICollection{AIContent}.Contains\" />\n    public bool Contains(AIContent item)\n    {\n        return this.Content.Any(x => x.AIContent == item);\n    }\n\n    /// <inheritdoc cref=\"ICollection{AIContent}.CopyTo\" />\n    public void CopyTo(AIContent[] array, int arrayIndex)\n    {\n        if (array == null)\n        {\n            throw new ArgumentNullException(nameof(array));\n        }\n\n        if (arrayIndex < 0 || arrayIndex >= array.Length)\n        {\n            throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n        }\n\n        if (array.Length - arrayIndex < this.Content.Count)\n        {\n            throw new ArgumentException(\"The number of elements in the source is greater than the available space from arrayIndex to the end of the destination array.\");\n        }\n\n        for (var i = 0; i < this.Content.Count; i++)\n        {\n            array[arrayIndex + i] = this.Content[i].AIContent;\n        }\n    }\n\n    /// <inheritdoc cref=\"IEnumerable{AIContent}.GetEnumerator\" />\n    public IEnumerator<AIContent> GetEnumerator()\n    {\n        return this.Content.Select(x => x.AIContent).GetEnumerator();\n    }","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/AgentChat/Abstractions/Messages.cs#L308-L344","documentation":"Bounds check in ICollection<AIContent>.CopyTo: arrayIndex must satisfy 0 <= arrayIndex < array.Length, otherwise ArgumentOutOfRangeException(nameof(arrayIndex)). Note it rejects arrayIndex == array.Length even for an empty collection, which is slightly stricter than some BCL implementations.","triggerScenarios":"Calling CopyTo(array, negativeIndex) or CopyTo(array, array.Length) (e.g. appending after existing items but passing length instead of a valid last index), or on an empty/zero-length array where any index fails.","commonSituations":"Index arithmetic bugs when copying into a larger buffer at an offset; passing array.Length by mistake when the intent was 'end of data so far'; zero-length arrays from empty allocations.","solutions":["Pass an index strictly inside the array: 0 <= arrayIndex < array.Length (use the count of already-filled elements, not array.Length)","Ensure the destination array is non-empty; skip CopyTo entirely when there is nothing to copy","Double-check offset arithmetic when copying multiple collections into one buffer"],"exampleFix":"// before\nmsg.Content.CopyTo(dest, dest.Length); // always out of range\n// after\nmsg.Content.CopyTo(dest, filled); // filled = number of elements already written","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex), \"must be 0 <= index < length\");\nmsg.Content.CopyTo(array, arrayIndex);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the count of already-written elements as the index, never array.Length","Skip CopyTo when there is nothing to copy (empty source or empty destination)","Wrap buffer offset math in one helper to avoid scattered index arithmetic"],"tags":["autogen","collections","copyto","bounds-check","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}