{"record":{"id":"46190982e1cc2b47","repo":"dotnet/wpf","slug":"notsupportedexception-typefacecollection","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/TypefaceCollection.cs","lineNumber":37,"sourceCode":"        public TypefaceCollection(FontFamily fontFamily, Text.TextInterface.FontFamily family)\n        {\n            _fontFamily = fontFamily;\n            _family = family;\n            _familyTypefaceCollection = null;\n        }\n\n        public TypefaceCollection(FontFamily fontFamily, FamilyTypefaceCollection familyTypefaceCollection)\n        {\n            _fontFamily = fontFamily;\n            _familyTypefaceCollection = familyTypefaceCollection;\n            _family = null;\n        }\n\n        #region ICollection<Typeface> Members\n\n        public void Add(Typeface item)\n        {\n            throw new NotSupportedException();\n        }\n\n        public void Clear()\n        {\n            throw new NotSupportedException();\n        }\n\n        public bool Contains(Typeface item)\n        {\n            foreach (Typeface t in this)\n            {\n                if (t.Equals(item))\n                    return true;\n            }\n            return false;\n        }\n\n        public void CopyTo(Typeface[] array, int arrayIndex)","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/TypefaceCollection.cs#L19-L55","documentation":"TypefaceCollection implements ICollection<Typeface> but is a read-only view: Add (and the other mutators) throw NotSupportedException. The collection only reflects the typefaces of an underlying font family; you cannot insert items into it.","triggerScenarios":"Calling Add/Remove/Clear on a TypefaceCollection obtained from a FontFamily (e.g. FontFamily.FamilyNames or FamilyTypefaces), or generic code that treats any ICollection<Typeface> as writable.","commonSituations":"LINQ/binding code or collection-initializer syntax accidentally trying to mutate the family's typeface list, porting code that worked with List<Typeface> to TypefaceCollection.","solutions":["Do not mutate TypefaceCollection — build a new List<Typeface> from it if modification is needed","Replace the target collection with a writable List<Typeface> or ObservableCollection<Typeface>","Guard writes with collection.IsReadOnly checks in generic code"],"exampleFix":"// before\ncollection.Add(new Typeface(\"Arial\")); // NotSupportedException\n// after\nvar editable = new List<Typeface>(collection) { new Typeface(\"Arial\") };","handlingStrategy":"type-guard","validationCode":"if (typefaceCollection.IsReadOnly)\n    copy = new List<Typeface>(typefaceCollection);","typeGuard":"bool IsWritableCollection<T>(ICollection<T> c) => c != null && !c.IsReadOnly;","tryCatchPattern":"try { collection.Add(item); }\ncatch (NotSupportedException)\n{\n    throw new InvalidOperationException(\"TypefaceCollection is read-only; copy to a List<Typeface> to edit\");\n}","preventionTips":["Check ICollection<T>.IsReadOnly before writing in generic code","Treat TypefaceCollection as a projection of the FontFamily, not storage","Avoid collection-initializer syntax on TypefaceCollection"],"tags":["wpf","fonts","read-only-collection","not-supported"],"backgroundTag":"operation-not-supported","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"}