stride3d/stride · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

SortedDictionary<TKey,TValue>.KeyCollection is a read-only view over the dictionary's keys; its explicit ICollection<TKey>.Add always throws NotSupportedException. You cannot add a key through the KeyCollection — keys are only created by adding entries to the dictionary itself.

Solutions

  1. Add to the dictionary itself: dict[key] = value; which creates the key.
  2. If a mutable key list is needed, copy first: var keys = new List<TKey>(dict.Keys); then keys.Add(...).
  3. Change generic helpers to accept IEnumerable<TKey> when mutation isn't intended, or document that callers must pass mutable collections.

Example fix

// before
ICollection<string> keys = dict.Keys;
keys.Add("newKey"); // NotSupportedException
// after
dict["newKey"] = value; // add via the dictionary
// or
var keys = new List<string>(dict.Keys);
keys.Add("newKey");
Defensive patterns

Strategy: type-guard

Type guard

static bool CanAdd<T>(ICollection<T> c) => !c.IsReadOnly;

Try / catch

try { keysCollection.Add(key); }
catch (NotSupportedException) { dict[key] = value; // add through the dictionary instead }

Prevention

When it happens

Trigger: Casting the Keys collection to ICollection<TKey> (or assigning it to an ICollection<TKey> variable) and calling Add(key), e.g. through generic code that adds to any ICollection<TKey> it receives.

Common situations: Generic helper methods that accept ICollection<TKey> and call Add, handed dict.Keys; LINQ-adjacent code paths that assume any ICollection is mutable; refactoring from List<TKey> (mutable) to dict.Keys (read-only view).

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 stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/241fa0ca37b16db1. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/SortedDictionary.cs:615

                        {
                            objects[index++] = node.Item.Key;
                            return true;
                        });
                    }
                    catch (ArrayTypeMismatchException)
                    {
                        throw new ArgumentException();
                    }
                }
            }

            public int Count { get { return dictionary.Count; } }

            bool ICollection<TKey>.IsReadOnly { get { return true; } }

            void ICollection<TKey>.Add(TKey item)
            {
                throw new NotSupportedException();
            }

            void ICollection<TKey>.Clear()
            {
                throw new NotSupportedException();
            }

            bool ICollection<TKey>.Contains(TKey item)
            {
                return dictionary.ContainsKey(item);
            }

            bool ICollection<TKey>.Remove(TKey item)
            {
                throw new NotSupportedException();
            }

            bool ICollection.IsSynchronized { get { return false; } }

View on GitHub (pinned to 96fad776d2)