{"record":{"id":"8e71ba904f3ce884","repo":"dotnet/wpf","slug":"sr-tablecollectioninvalidofflen","errorCode":null,"errorMessage":"SR.TableCollectionInvalidOffLen","messagePattern":"SR\\.TableCollectionInvalidOffLen","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs","lineNumber":77,"sourceCode":"        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <param name=\"array\"><see cref=\"ICollection.CopyTo\"/></param>\n        /// <param name=\"index\"><see cref=\"ICollection.CopyTo\"/></param>\n        public void CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(SR.TableCollectionRankMultiDimNotSupported);\n            }\n            if (index < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), SR.TableCollectionOutOfRangeNeedNonNegNum);\n            }\n            if (array.Length - index < Size)\n            {\n                throw new ArgumentException(SR.TableCollectionInvalidOffLen);\n            }\n\n            Array.Copy(Items, 0, array, index, Size);\n        }\n\n        /// <summary>\n        /// Strongly typed version of ICollection.CopyTo.\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <param name=\"array\"><see cref=\"ICollection.CopyTo\"/></param>","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs#L59-L95","documentation":"ContentElementCollection.CopyTo validates that the destination array has enough room: array.Length - index must be at least Size (the collection count). When available space is insufficient it throws ArgumentException with TableCollectionInvalidOffLen. This prevents Array.Copy from failing partially or unexpectedly and enforces the ICollection.CopyTo contract.","triggerScenarios":"Calling CopyTo with an array whose Length minus index is smaller than the collection count, e.g. collection.CopyTo(new ContentElement[2], 0) on a 5-element collection, or CopyTo(array, array.Length - 1) leaving only one slot.","commonSituations":"Sizing the array from a stale Count captured before the collection changed; reusing a cached buffer across collections of different sizes; copying at an offset into a right-sized array.","solutions":["Re-read collection.Count and allocate the array immediately before CopyTo","Verify array.Length - index >= collection.Count before calling, throwing a clear error or resizing","Use ToArray()/ToList() to let the framework allocate correctly sized storage"],"exampleFix":"// before\nvar arr = new ContentElement[2];\ncollection.CopyTo(arr, 0);\n// after\nvar arr = new ContentElement[collection.Count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"int required = collection.Count + index;\nif (array.Length < required) array = new ContentElement[required];\ncollection.CopyTo(array, index);","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, index); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"OffLen\"))\n{\n    var sized = new ContentElement[collection.Count];\n    collection.CopyTo(sized, 0);\n}","preventionTips":["Allocate the destination array immediately before CopyTo using the current Count","Re-check Count if the collection may change between sizing and copying","Prefer ToArray()/ToList() which size automatically"],"tags":["wpf","argument-exception","copyto","array-capacity"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}