{"record":{"id":"e07eacdf2debccec","repo":"stride3d/stride","slug":"attempt-to-modify-a-key","errorCode":null,"errorMessage":"attempt to modify a key","messagePattern":"attempt to modify a key","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":996,"sourceCode":"        public virtual void Insert(int index, TKey item)\n        {\n            throw new NotSupportedException();\n        }\n\n        public virtual void RemoveAt(int index)\n        {\n            throw new NotSupportedException();\n        }\n\n        public virtual TKey this[int index]\n        {\n            get\n            {\n                return host.KeyAt(index);\n            }\n            set\n            {\n                throw new NotSupportedException(\"attempt to modify a key\");\n            }\n        }\n\n        //\n        // IEnumerable<TKey>\n        //\n\n        public virtual IEnumerator<TKey> GetEnumerator()\n        {\n            /* We couldn't use yield as it does not support Reset () */\n            return new KeyEnumerator(host);\n        }\n\n        //\n        // ICollection\n        //\n\n        public virtual int Count => host.Count;","sourceCodeStart":978,"sourceCodeEnd":1014,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L978-L1014","documentation":"The key list returned by SortedList.Keys is a read-only view; its indexer setter throws NotSupportedException with 'attempt to modify a key' because changing a key in place would break the sorted-order invariant of the list.","triggerScenarios":"Assigning through the key view's indexer, e.g. sortedList.Keys[i] = newKey, or via the IList interface cast.","commonSituations":"Trying to 'fix' a wrong key in place; code ported from a mutable IList<TKey> usage; misunderstanding that Keys is a live read-only projection.","solutions":["Remove the old entry and add the corrected one: sortedList.RemoveAt(i); sortedList[newKey] = value;","Treat Keys as read-only; mutate entries only through the SortedList itself","If bulk key updates are needed, rebuild the SortedList from corrected pairs"],"exampleFix":"// before\nsortedList.Keys[i] = newKey; // throws\n// after\nvar value = sortedList.Values[i];\nsortedList.RemoveAt(i);\nsortedList[newKey] = value;","handlingStrategy":"validation","validationCode":"bool isReadOnlyKeyView(IList<TKey> keys) => keys is System.Collections.IList l && l.IsReadOnly;","typeGuard":"static bool IsReadOnlyKeyView<TK,TV>(SortedList<TK,TV> list, IList<TK> view) => view == list.Keys;","tryCatchPattern":"try { keysView[i] = newKey; } catch (NotSupportedException) { var v = list.Values[i]; list.RemoveAt(i); list[newKey] = v; }","preventionTips":["Treat Keys/Values views as read-only always","Change a key via RemoveAt + re-insert","Never cast the key view to IList and write through it"],"tags":["notsupportedexception","readonly","sortedlist","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"}