dotnet/wpf · error · NotSupportedException

CollectionIsFixedSize

Error message

CollectionIsFixedSize

What it means

ThousandthOfEmRealDoubles.Add throws NotSupportedException(SR.CollectionIsFixedSize). The collection is a fixed-size wrapper (like double[]) — it models a read-only-size sequence of glyph-metric doubles, so mutation via Add is deliberately unsupported.

Solutions

  1. Do not mutate; construct a new ThousandthOfEmRealDoubles with the required capacity and populate via the constructor/init path
  2. Use List<double> (or double[]) if you need Add semantics, then create the collection from it
  3. CheckICollection<double>.IsFixedSize before attempting mutation in generic code

Example fix

// before
collection.Add(0.25);
// after
var list = new List<double>(collection) { 0.25 };
var fixedCollection = new ThousandthOfEmRealDoubles(list, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (((ICollection<double>)collection).IsFixedSize)
    throw new InvalidOperationException("collection does not support Add");

Type guard

static bool SupportsAdd(ICollection<double> c) => !c.IsFixedSize;

Try / catch

try { ((ICollection<double>)collection).Add(v); }
catch (NotSupportedException ex)
{ /* rebuild collection with new capacity instead */ }

Prevention

When it happens

Trigger: Calling Add(double) directly or through ICollection<double>/IList interfaces, e.g. ((ICollection<double>)collection).Add(0.5).

Common situations: Generic algorithms that treat any ICollection<T> as growable; LINQ/buffer-copying code paths that append; porting code that used List<double>.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7581c65a682a4bb8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs:220

        public IEnumerator<double> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return this[i];
            }
        }        

	System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<double>)this).GetEnumerator();
        }


        public void Add(double value)
        {
            // not supported, same as double[] 
            throw new NotSupportedException(SR.CollectionIsFixedSize);                           
        }

        public void Insert(int index, double item)
        {
            // not supported, same as double[] 
            throw new NotSupportedException(SR.CollectionIsFixedSize);                           
        }

        public bool Remove(double item)
        {
            // not supported, same as double[]             
            throw new NotSupportedException(SR.CollectionIsFixedSize);                           
        }

        public void RemoveAt(int index)
        {
            // not supported, same as double[]             
            throw new NotSupportedException(SR.CollectionIsFixedSize);                           

View on GitHub (pinned to 81131a70a4)