{"record":{"id":"241fa0ca37b16db1","repo":"stride3d/stride","slug":"notsupportedexception","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/SortedDictionary.cs","lineNumber":615,"sourceCode":"                        {\n                            objects[index++] = node.Item.Key;\n                            return true;\n                        });\n                    }\n                    catch (ArrayTypeMismatchException)\n                    {\n                        throw new ArgumentException();\n                    }\n                }\n            }\n\n            public int Count { get { return dictionary.Count; } }\n\n            bool ICollection<TKey>.IsReadOnly { get { return true; } }\n\n            void ICollection<TKey>.Add(TKey item)\n            {\n                throw new NotSupportedException();\n            }\n\n            void ICollection<TKey>.Clear()\n            {\n                throw new NotSupportedException();\n            }\n\n            bool ICollection<TKey>.Contains(TKey item)\n            {\n                return dictionary.ContainsKey(item);\n            }\n\n            bool ICollection<TKey>.Remove(TKey item)\n            {\n                throw new NotSupportedException();\n            }\n\n            bool ICollection.IsSynchronized { get { return false; } }","sourceCodeStart":597,"sourceCodeEnd":633,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/SortedDictionary.cs#L597-L633","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Add to the dictionary itself: dict[key] = value; which creates the key.","If a mutable key list is needed, copy first: var keys = new List<TKey>(dict.Keys); then keys.Add(...).","Change generic helpers to accept IEnumerable<TKey> when mutation isn't intended, or document that callers must pass mutable collections."],"exampleFix":"// before\nICollection<string> keys = dict.Keys;\nkeys.Add(\"newKey\"); // NotSupportedException\n// after\ndict[\"newKey\"] = value; // add via the dictionary\n// or\nvar keys = new List<string>(dict.Keys);\nkeys.Add(\"newKey\");","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static bool CanAdd<T>(ICollection<T> c) => !c.IsReadOnly;","tryCatchPattern":"try { keysCollection.Add(key); }\ncatch (NotSupportedException) { dict[key] = value; // add through the dictionary instead }","preventionTips":["Treat dict.Keys as IReadOnlyCollection<TKey> — never store it in an ICollection<TKey> variable","Add new keys via the dictionary indexer or Add method, not the Keys view","In generic code, check IsReadOnly before calling Add"],"tags":["read-only-collection","not-supported","keys"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}