{"record":{"id":"fa4053b979b94f96","repo":"dotnet/wpf","slug":"sr-itemcollectionremoveargumentoutofrange-fa4053","errorCode":null,"errorMessage":"SR.ItemCollectionRemoveArgumentOutOfRange","messagePattern":"SR\\.ItemCollectionRemoveArgumentOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CompositeCollection.cs","lineNumber":242,"sourceCode":"        public void RemoveAt(int removeIndex)\n        {\n            if ((0 <= removeIndex) && (removeIndex < Count))\n            {\n                object removedItem = this[removeIndex];\n\n                CollectionContainer cc = removedItem as CollectionContainer;\n                if (cc != null)\n                {\n                    RemoveCollectionContainer(cc);\n                }\n\n                InternalList.RemoveAt(removeIndex);\n\n                OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, removeIndex);\n            }\n            else\n            {\n                throw new ArgumentOutOfRangeException(nameof(removeIndex),\n                            SR.ItemCollectionRemoveArgumentOutOfRange);\n            }\n        }\n\n\n        /// <summary>\n        /// Create a new view on this collection [Do not call directly].\n        /// </summary>\n        /// <remarks>\n        /// Normally this method is only called by the platform's view manager,\n        /// not by user code.\n        /// </remarks>\n        ICollectionView ICollectionViewFactory.CreateView()\n        {\n            return new CompositeCollectionView(this);\n        }\n\n        #endregion Public Methods","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CompositeCollection.cs#L224-L260","documentation":"CompositeCollection.RemoveAt validates the index before removing. If removeIndex does not point at a valid item in the composite list (out of the range covering all contained collections and the private items list), it throws ArgumentOutOfRangeException with ItemCollectionRemoveArgumentOutOfRange.","triggerScenarios":"Calling compositeCollection.RemoveAt(i) where i >= Count or i < 0; stale index capture before the composite shrank; index computed against the inner collection instead of the composite.","commonSituations":"Iterating a CompositeCollection while removing items without adjusting indices; removing by an index obtained from an inner ObservableCollection's position rather than the composite's flattened index space.","solutions":["Validate 0 <= index && index < compositeCollection.Count before calling RemoveAt","Remove items by reference where possible, or recompute the composite index","Avoid removing inside a forward loop; iterate backwards or re-fetch Count each iteration"],"exampleFix":"// before\ncomposite.RemoveAt(innerIndex); // index from inner collection\n// after\nif (index >= 0 && index < composite.Count)\n    composite.RemoveAt(index);","handlingStrategy":"validation","validationCode":"bool canRemoveAt(CompositeCollection c, int i) => i >= 0 && i < c.Count;","typeGuard":"bool TryRemoveAt(CompositeCollection c, int i, out Exception err) { err = null; if (i < 0 || i >= c.Count) { err = new ArgumentOutOfRangeException(nameof(i)); return false; } return true; }","tryCatchPattern":"try { composite.RemoveAt(index); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"removeIndex\") { /* refresh index and retry or log */ }","preventionTips":["Re-read Count before each removal in loops","Convert inner-collection indices to composite indices before RemoveAt","Iterate backwards when removing multiple items"],"tags":["wpf","data-binding","compositecollection","argument-out-of-range"],"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"}