{"record":{"id":"0635bfa035907ef4","repo":"dotnet/wpf","slug":"sr-tablecollectionoutofrange-0635bf","errorCode":null,"errorMessage":"SR.TableCollectionOutOfRange","messagePattern":"SR\\.TableCollectionOutOfRange","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs","lineNumber":104,"sourceCode":"        /// </exception>\n        /// <remarks>\n        /// If Count already equals Capacity, the capacity of the\n        /// ContentElementCollection is increased before the new TItem is inserted.\n        ///\n        /// If index is equal to Count, TItem is added to the\n        /// end of ContentElementCollection.\n        ///\n        /// The TItems that follow the insertion point move down to\n        /// accommodate the new TItem. The indexes of the TItems that are\n        /// moved are also updated.\n        /// </remarks>\n        public override void Insert(int index, TElementType item)\n        {\n            Version++;\n\n            if (index < 0 || index > Size)\n            {\n                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);\n            }\n            ArgumentNullException.ThrowIfNull(item);\n\n            if (item.Parent != null)\n            {\n                throw new System.ArgumentException(SR.TableCollectionInOtherCollection);\n            }\n\n            Owner.InsertionIndex = index;\n            if (index == Size)\n            {\n                item.RepositionWithContent(Owner.ContentEnd);\n            }\n            else\n            {\n                TElementType itemInsert = Items[index];\n                TextPointer insertPosition = new TextPointer(itemInsert.ContentStart, -1);\n                item.RepositionWithContent(insertPosition);","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs#L86-L122","documentation":"TableTextElementCollectionInternal<T>.Insert validates that index is within [0, Size] before inserting. This ArgumentOutOfRangeException is thrown when the insertion position is negative or greater than the current item count. Unlike some collections, inserting exactly at Size (append position) is allowed.","triggerScenarios":"Insert(index, item) with index < 0; index > Size, e.g. Insert(Count, ...) after items were removed (stale count), or inserting at a computed position derived from another collection's size.","commonSituations":"Inserting at a saved cursor position after the document changed, using an index from one collection on a different collection, and off-by-one errors when index was intended as 'append' but the collection shrank.","solutions":["Clamp the index: index = Math.Max(0, Math.Min(index, collection.Count)).","Re-read Count immediately before inserting instead of caching an old value.","Use Add when the intent is to append at the end.","Guard with if (index >= 0 && index <= collection.Count) before calling."],"exampleFix":"// before\ncollection.Insert(savedIndex, item); // savedIndex may be stale\n// after\nint index = Math.Min(savedIndex, collection.Count);\ncollection.Insert(Math.Max(0, index), item);","handlingStrategy":"validation","validationCode":"index = Math.Max(0, Math.Min(index, collection.Count));\ncollection.Insert(index, item);","typeGuard":"static bool IsValidInsertIndex(ICollection c, int i) => i >= 0 && i <= c.Count;","tryCatchPattern":"try { collection.Insert(index, item); }\ncatch (ArgumentOutOfRangeException) { collection.Add(item); }","preventionTips":["Clamp index into [0, Count]","Use Add for appends","Refresh Count after any prior edit before inserting"],"tags":["wpf","argument-out-of-range","table","collection"],"backgroundTag":"index-out-of-bounds","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"}