{"record":{"id":"ed360a831eb789d1","repo":"AvaloniaUI/Avalonia","slug":"specified-argument-was-out-of-the-range-of-valid-v-ed360a","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":"src/Avalonia.Base/Reactive/CompositeDisposable.cs","lineNumber":301,"sourceCode":"    }\n\n    /// <summary>\n    /// Copies the disposables contained in the <see cref=\"CompositeDisposable\"/> to an array, starting at a particular array index.\n    /// </summary>\n    /// <param name=\"array\">Array to copy the contained disposables to.</param>\n    /// <param name=\"arrayIndex\">Target index at which to copy the first disposable of the group.</param>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"array\"/> is <c>null</c>.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"arrayIndex\"/> is less than zero. -or - <paramref name=\"arrayIndex\"/> is larger than or equal to the array length.</exception>\n    public void CopyTo(IDisposable[] 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        lock (_gate)\n        {\n            // disposed composites are always empty\n            if (_disposed)\n            {\n                return;\n            }\n\n            if (arrayIndex + _count > array.Length)\n            {\n                // there is not enough space beyond arrayIndex \n                // to accommodate all _count disposables in this composite\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n            }\n\n            var i = arrayIndex;","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Reactive/CompositeDisposable.cs#L283-L319","documentation":"CompositeDisposable.CopyTo performs an upfront bounds check: if arrayIndex < 0 OR arrayIndex >= array.Length it throws ArgumentOutOfRangeException(nameof(arrayIndex)). This catches negative indices and an index at/past the end of a (possibly empty) array before copying.","triggerScenarios":"Calling composite.CopyTo(arr, -1) or composite.CopyTo(arr, arr.Length), or CopyTo into an empty array with arrayIndex 0 (0 >= 0 length triggers it).","commonSituations":"Indexing into a zero-length array, or computing arrayIndex as a length that equals the array size, or passing a stale index after the array was resized down.","solutions":["Validate arrayIndex is within [0, array.Length) and the array is large enough before calling.","Allocate an array sized at least composite.Count and always start at index 0 unless you intentionally offset.","Branch on array.Length == 0 to avoid the degenerate case."],"exampleFix":"// before\ncomposite.CopyTo(arr, arr.Length); // index >= length -> throws\n\n// after\nvar arr = new IDisposable[composite.Count];\ncomposite.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array is null || arrayIndex < 0 || arrayIndex >= array.Length) return;\ncomposite.CopyTo(array, arrayIndex);","typeGuard":"bool ValidIndex(IDisposable[] a, int i) => a is not null && i >= 0 && i < a.Length;","tryCatchPattern":null,"preventionTips":["Validate arrayIndex is within [0, array.Length).","Allocate arrays sized to composite.Count and copy from 0.","Handle the zero-length array case explicitly."],"tags":["avalonia","reactive","disposables","argumentoutofrange","copyto","bounds"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}