{"record":{"id":"4ee91d7de5963b02","repo":"AvaloniaUI/Avalonia","slug":"destination-span-is-shorter-than-the-list-to-be-co","errorCode":null,"errorMessage":"Destination span is shorter than the list to be copied.","messagePattern":"Destination span is shorter than the list to be copied\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/Pooled/PooledList.cs","lineNumber":647,"sourceCode":"                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);\n            }\n\n            var list = new PooledList<TOutput>(_size);\n            for (int i = 0; i < _size; i++)\n            {\n                list._items[i] = converter(_items[i]);\n            }\n            list._size = _size;\n            return list;\n        }\n\n        /// <summary>\n        /// Copies this list to the given span.\n        /// </summary>\n        public void CopyTo(Span<T> span)\n        {\n            if (span.Length < Count)\n                throw new ArgumentException(\"Destination span is shorter than the list to be copied.\");\n\n            Span.CopyTo(span);\n        }\n\n        void ICollection<T>.CopyTo(T[] array, int arrayIndex)\n        {\n            Array.Copy(_items, 0, array, arrayIndex, _size);\n        }\n\n        // Copies this List into array, which must be of a \n        // compatible array type.  \n        void ICollection.CopyTo(Array array, int arrayIndex)\n        {\n            _ = array ?? throw new ArgumentNullException(nameof(array));\n\n            if (array.Rank != 1)\n            {\n                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);","sourceCodeStart":629,"sourceCodeEnd":665,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/Pooled/PooledList.cs#L629-L665","documentation":"ArgumentException thrown by PooledList<T>.CopyTo(Span<T>) when the destination span is not large enough to hold every element (`span.Length < Count`). It is a precondition check before Span.CopyTo, guaranteeing the copy cannot run out of room. Unlike the array CopyTo overload it reports the failure immediately with a clear message rather than via Array.Copy.","triggerScenarios":"Calling `list.CopyTo(span)` with a span whose Length is smaller than list.Count — e.g. a stackalloc buffer, a rented array sliced too short, or an array allocated with the wrong size.","commonSituations":"Sizing a destination buffer to an outdated Count; off-by-one when slicing; copying into a span obtained from a smaller pool rental; assuming Count==Capacity.","solutions":["Allocate the destination to exactly Count: `Span<T> buf = new T[list.Count]; list.CopyTo(buf);`.","Use the `list.Span` accessor directly to read elements without copying at all.","Switch to the array-based CopyTo overload if you already have a correctly-sized array."],"exampleFix":"// before\nSpan<T> buf = new T[list.Capacity]; // wrong: too small if Capacity grew\nlist.CopyTo(buf);\n\n// after\nSpan<T> buf = new T[list.Count];\nlist.CopyTo(buf);","handlingStrategy":"validation","validationCode":"if (span.Length < list.Count)\n    throw new InvalidOperationException(\"Buffer too small\");\nlist.CopyTo(span);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always size the destination span to list.Count, never Capacity.","Prefer reading list.Span directly to avoid the copy entirely.","Keep Count fresh — re-read it right before allocating the buffer."],"tags":["csharp","pooled","span","copy","collections"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}