{"record":{"id":"9082701c21033ee5","repo":"dotnet/wpf","slug":"throw-new-notsupportedexception-in-icollection-t-remove","errorCode":null,"errorMessage":"throw new NotSupportedException() in ICollection<T>.Remove (WeakReferenceList)","messagePattern":"throw new NotSupportedException\\(\\) in ICollection<T>\\.Remove \\(WeakReferenceList\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs","lineNumber":1467,"sourceCode":"                    {\n                        return true;\n                    }\n                }\n\n                return false;\n            }\n\n            void ICollection<T>.CopyTo(T[] array, int arrayIndex)\n            {\n                for (int i = 0; i < Count; i++)\n                {\n                    array[i + arrayIndex] = (T)this[i].Target;\n                }\n            }\n\n            bool ICollection<T>.Remove(T item)\n            {\n                throw new NotSupportedException();\n            }\n\n            bool ICollection<T>.IsReadOnly\n            {\n                get { return false; }\n            }\n\n            IEnumerator<T> IEnumerable<T>.GetEnumerator()\n            {\n                return Enumerate().GetEnumerator();\n            }\n\n            IEnumerator IEnumerable.GetEnumerator()\n            {\n                return ((IEnumerable<T>)this).GetEnumerator();\n            }\n\n            private IEnumerable<T> Enumerate()","sourceCodeStart":1449,"sourceCodeEnd":1485,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs#L1449-L1485","documentation":"WeakReferenceList implements ICollection<T> as a read-style collection of weak references; item removal via the explicit ICollection<T>.Remove interface method is intentionally not supported and always throws NotSupportedException. The collection is designed to be mutated only through its own internal mechanisms (appending, compaction of dead weak references), not by callers removing arbitrary items.","triggerScenarios":"Calling Remove(item) on a WeakReferenceList instance through the ICollection<T> interface, e.g. casting it to ICollection<T> and calling Remove, or passing it to code that uses the generic collection interface to remove elements. Calling the non-interface Remove overloads of the class does not hit this path.","commonSituations":"Developers treat the list as a normal ICollection<T> in LINQ/algorithm code and attempt in-place element removal; reflection-based or serialization code enumerates ICollection<T> members and invokes Remove; code refactored from List<T> to WeakReferenceList assumes removal is supported.","solutions":["Do not remove items via ICollection<T>.Remove; rebuild the list without the unwanted item instead","Use the type's own supported mutation methods (Add/internal compaction) rather than interface-based removal","Copy elements to a List<T> (skipping the unwanted item or dead references), operate on that, and rebuild the WeakReferenceList","If you own the call site, check ICollection<T>.IsReadOnly semantics and avoid Remove on this collection type"],"exampleFix":"// before\nICollection<WeakReference> list = weakRefList;\nlist.Remove(item); // throws NotSupportedException\n// after\nvar keep = new List<WeakReference>();\nfor (int i = 0; i < weakRefList.Count; i++)\n    if (!ReferenceEquals(weakRefList[i], item)) keep.Add(weakRefList[i]);\n// rebuild/reassign from `keep` using supported APIs","handlingStrategy":"try-catch","validationCode":"if (coll is ICollection<T> icoll && icoll.Contains(item)) { /* Remove unsupported: rebuild instead */ }","typeGuard":"bool supportsRemove = collection is ICollection<T> && !(collection.GetType().Name == \"WeakReferenceList\");","tryCatchPattern":"try { ((ICollection<T>)weakRefList).Remove(item); }\ncatch (NotSupportedException) { /* rebuild list without item via supported APIs */ }","preventionTips":["Treat WeakReferenceList as append/inspect-only; never mutate via ICollection<T>","Check SupportedCapabilities/IsReadOnly conventions before interface-based removal","Copy to List<T> for element-level operations"],"tags":["notsupported","collection","xaml","weakreference"],"backgroundTag":"unsupported-operation","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"}