{"record":{"id":"2a82386ff2eaf6ef","repo":"dotnet/wpf","slug":"notsupportedexception-sortdescriptioncollection","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescriptionCollection.cs","lineNumber":143,"sourceCode":"        /// Immutable, read-only SortDescriptionCollection\n        /// </summary>\n        private class EmptySortDescriptionCollection : SortDescriptionCollection, IList\n        {\n            //------------------------------------------------------\n            //\n            //  Protected Methods\n            //\n            //------------------------------------------------------\n\n            #region Protected Methods\n\n            /// <summary>\n            /// called by base class Collection&lt;T&gt; when the list is being cleared;\n            /// raises a CollectionChanged event to any listeners\n            /// </summary>\n            protected override void ClearItems()\n            {\n                throw new NotSupportedException();\n            }\n\n            /// <summary>\n            /// called by base class Collection&lt;T&gt; when an item is removed from list;\n            /// raises a CollectionChanged event to any listeners\n            /// </summary>\n            protected override void RemoveItem(int index)\n            {\n                throw new NotSupportedException();\n            }\n\n            /// <summary>\n            /// called by base class Collection&lt;T&gt; when an item is added to list;\n            /// raises a CollectionChanged event to any listeners\n            /// </summary>\n            protected override void InsertItem(int index, SortDescription item)\n            {\n                throw new NotSupportedException();","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescriptionCollection.cs#L125-L161","documentation":"SortDescriptionCollection is a read-only collection: ClearItems (called by Collection<T>.Clear) always throws NotSupportedException. The collection only supports adding via its public API and removal through ReplaceItem/clearing semantics defined by the view, not direct Clear().","triggerScenarios":"Calling myView.SortDescriptions.Clear() or otherwise triggering ICollection<T>.Clear on a SortDescriptionCollection instance.","commonSituations":"Resetting sort criteria before applying new ones in WPF collection-view code.","solutions":["Remove items individually with RemoveAt/Remove (if the view allows) or replace the whole collection reference.","Rebuild sorting by assigning a fresh SortDescriptionCollection or by replacing items in-place via the indexer.","Wrap the Clear call in a loop replacing entries, or clear via ICollectionView by setting a new SortDescriptions set."],"exampleFix":"// before\nview.SortDescriptions.Clear(); // throws\n// after\nwhile (view.SortDescriptions.Count > 0) view.SortDescriptions.RemoveAt(view.SortDescriptions.Count - 1);","handlingStrategy":"validation","validationCode":"if (view.SortDescriptions is SortDescriptionCollection && view.SortDescriptions.Count > 0) { /* clear via RemoveAt loop or rebuild */ }","typeGuard":null,"tryCatchPattern":"try { view.SortDescriptions.Clear(); } catch (NotSupportedException) { /* clear via RemoveAt loop */ }","preventionTips":["Remember SortDescriptionCollection is effectively write-once via Add","Rebuild the collection rather than clearing it"],"tags":["wpf","sortdescriptioncollection","readonly-collection","notsupported"],"backgroundTag":"unsupported-operation","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"}