{"record":{"id":"6f6fcae6b4ad5995","repo":"dotnet/wpf","slug":"throw-new-notsupportedexception","errorCode":null,"errorMessage":"throw new NotSupportedException();","messagePattern":"throw new NotSupportedException\\(\\);","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/SequentialUshortCollection.cs","lineNumber":66,"sourceCode":"            ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);\n\n            for (ushort i = 0; i < _count; ++i)\n                array[arrayIndex + i] = i;\n        }\n\n        public int Count\n        {\n            get { return _count; }\n        }\n\n        public bool IsReadOnly\n        {\n            get { return true; }\n        }\n\n        public bool Remove(ushort item)\n        {\n            throw new NotSupportedException();\n        }\n\n        #endregion\n\n        #region IEnumerable<ushort> Members\n\n        public IEnumerator<ushort> GetEnumerator()\n        {\n            for (ushort i = 0; i < _count; ++i)\n                yield return i;\n        }\n\n        #endregion\n\n        #region IEnumerable Members\n\n        IEnumerator IEnumerable.GetEnumerator()\n        {","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/SequentialUshortCollection.cs#L48-L84","documentation":"SequentialUshortCollection implements ICollection<ushort> as a read-only, run-length-encoded collection, so Remove(ushort) is not a supported operation and always throws NotSupportedException. Mutating this collection is impossible by design.","triggerScenarios":"Calling Remove(item), or LINQ/`List.Remove`-style mutation paths (e.g. ICollection<ushort>.Remove via interface) on a SequentialUshortCollection obtained from WPF (e.g. structure related to TextSource/character runs).","commonSituations":"Attempting to edit a collection that the API hands out as read-only; generic code that assumes any ICollection<T> is mutable.","solutions":["Do not mutate the collection; build a new List<ushort> from it and edit that","Check the IsReadOnly property (returns true) before mutating","Copy items into your own collection type if modification is required","Cast defensively: if collection.IsReadOnly, skip remove logic"],"exampleFix":"// before\nushortCollection.Remove(5);\n// after\nvar editable = new List<ushort>(ushortCollection);\neditable.Remove(5);","handlingStrategy":"type-guard","validationCode":"static bool CanRemove(ICollection<ushort> c) => !c.IsReadOnly;","typeGuard":"static List<ushort> AsEditable(IEnumerable<ushort> src) => new List<ushort>(src);","tryCatchPattern":"try { c.Remove(item); }\ncatch (NotSupportedException) { /* collection is read-only: copy and edit */ }","preventionTips":["Check IsReadOnly before any ICollection mutation","Treat SequentialUshortCollection as immutable","Copy into List<ushort> for edits"],"tags":["wpf","read-only","not-supported","collection"],"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"}