{"record":{"id":"dcf168c2ea21695d","repo":"dotnet/wpf","slug":"sr-tablecollectionoutofrange","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/TableColumnCollectionInternal.cs","lineNumber":99,"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, TableColumn item)\n        {\n            Version++;\n\n            if (index < 0 || index > Size)\n            {\n                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);\n            }\n            ArgumentNullException.ThrowIfNull(item);\n            if (Size == Items.Length)\n            {\n                EnsureCapacity(Size + 1);\n            }\n\n            for (int i = Size - 1; i >= index; --i)\n            {\n                Debug.Assert(BelongsToOwner(Items[i]));\n\n                Items[i + 1] = Items[i];\n                Items[i].Index = i + 1;\n            }\n\n            Items[index] = null;\n\n            Size++;","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs#L81-L117","documentation":"TableColumnCollectionInternal.Insert validates the insertion index against the collection bounds before inserting a TableColumn. An index below 0 or greater than Size (current count) throws ArgumentOutOfRangeException(SR.TableCollectionOutOfRange). The guard ensures the Table's column collection stays contiguous.","triggerScenarios":"Calling Insert with index < 0 or index > Count on a TableColumnCollection (e.g. inserting at Count+1 to 'append').","commonSituations":"Computing the insertion index from a loop or saved value that has become stale after prior removals; off-by-one when intending to append (append should use index == Count or Add); UI-driven index values that are -1 (e.g. 'no selection').","solutions":["Clamp the index to [0, Count] before calling Insert","Use Add() instead of Insert() when appending a column","Re-read the collection Count immediately before inserting rather than caching it","For -1 sentinel values from selection UI, convert them to Count or skip the insert"],"exampleFix":"// before\ncolumns.Insert(selectedIndex, newColumn);\n// after\nint idx = Math.Max(0, Math.Min(selectedIndex, columns.Count));\ncolumns.Insert(idx, newColumn);","handlingStrategy":"validation","validationCode":"bool canInsert = index >= 0 && index <= columns.Count;","typeGuard":null,"tryCatchPattern":"try { columns.Insert(index, col); } catch (ArgumentOutOfRangeException) { columns.Add(col); }","preventionTips":["Use Add() to append instead of Insert(Count+1, ...)","Clamp UI-derived indices (especially -1 'no selection') before inserting","Re-read Count just before the insert"],"tags":["wpf","tables","argument-out-of-range","collections"],"backgroundTag":"argument-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"}