{"record":{"id":"6729e75fb247e0eb","repo":"dotnet/wpf","slug":"throw-new-argumentexception-sr-copytonotenoughspace-nameof","errorCode":null,"errorMessage":"throw new ArgumentException(SR.CopyToNotEnoughSpace, nameof(index));","messagePattern":"throw new ArgumentException\\(SR\\.CopyToNotEnoughSpace, nameof\\(index\\)\\);","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs","lineNumber":320,"sourceCode":"            {\n                ic.CopyTo(array, index);\n            }\n            else\n            {\n                IList list = (IList)array;\n\n                foreach (object item in collection)\n                {\n                    if (index < array.Length)\n                    {\n                        list[index] = item;\n                        ++index;\n                    }\n                    else\n                    {\n                        // The number of elements in the source ICollection is greater than\n                        // the available space from index to the end of the destination array.\n                        throw new ArgumentException(SR.CopyToNotEnoughSpace, nameof(index));\n                    }\n                }\n            }\n        }\n\n\n        internal void Invalidate()\n        {\n            ClearAllCaches();\n\n            // only track changes if source collection isn't already of type IList\n            if (List == null)\n            {\n                INotifyCollectionChanged icc = Enumerable as INotifyCollectionChanged;\n                if (icc != null)\n                {\n                    CollectionChangedEventManager.RemoveHandler(icc, OnCollectionChanged);\n                }","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs#L302-L338","documentation":"IndexedEnumerable.CopyTo(array, index) enumerates the source and writes into the destination array. If the source has more elements than there is room for from 'index' to the end of the array, the copy cannot complete and ArgumentException (paramName 'index', message CopyToNotEnoughSpace) is thrown.","triggerScenarios":"Calling CopyTo with an array whose length minus 'index' is smaller than the number of items in the underlying enumerable — e.g. an array sized to Count before the collection grew, or a nonzero start index on a tight array.","commonSituations":"Caching collection contents into a preallocated array while the collection is concurrently appended to; copying with a nonzero arrayIndex without accounting for remaining capacity.","solutions":["Size the destination array to source Count + index (array = new T[index + count]).","Re-read Count immediately before allocating, on the same thread that mutates the collection.","Pass arrayIndex 0 when copying the whole collection into a fresh array.","Wrap the copy in a lock/measure-then-copy pattern if the source can change concurrently."],"exampleFix":"// before\nvar arr = new object[count];\nenumerable.CopyTo(arr, 5); // needs count+5 slots\n// after\nvar arr = new object[count + 5];\nenumerable.CopyTo(arr, 5);","handlingStrategy":"validation","validationCode":"int needed = index + count;\nif (array.Length - index < count)\n    array = new object[needed];","typeGuard":null,"tryCatchPattern":"try { e.CopyTo(arr, index); }\ncatch (ArgumentException ex) when (ex.ParamName == \"index\") { arr = new object[index + e.Count]; e.CopyTo(arr, index); }","preventionTips":["Allocate array as new T[index + sourceCount].","Re-measure Count immediately before copying.","Avoid nonzero arrayIndex unless capacity is verified.","Synchronize against concurrent appends during copy."],"tags":["wpf","copyto","array","insufficient-space"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}