{"record":{"id":"5694c0eb74cdfb7f","repo":"stride3d/stride","slug":"the-given-key-was-not-present-in-the-dictionary","errorCode":null,"errorMessage":"The given key was not present in the dictionary.","messagePattern":"The given key was not present in the dictionary\\.","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Design/Collections/HybridDictionary.cs","lineNumber":290,"sourceCode":"        CheckInvariant();\n        return (IEnumerator<KeyValuePair<TKey, TValue>>?)list?.GetEnumerator() ?? dictionary!.GetEnumerator();\n    }\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }\n\n    private TValue Fetch(TKey key)\n    {\n        if (list != null)\n        {\n            foreach (var kvp in list)\n            {\n                if (keyComparer.Equals(kvp.Key, key))\n                    return kvp.Value;\n            }\n            throw new KeyNotFoundException(\"The given key was not present in the dictionary.\");\n        }\n        return dictionary![key];\n    }\n\n    private void Update(TKey key, TValue value)\n    {\n        valueCollection = null;\n        if (list != null)\n        {\n            for (var i = 0; i < list.Count; i++)\n            {\n                var kvp = list[i];\n                if (keyComparer.Equals(kvp.Key, key))\n                {\n                    list[i] = new KeyValuePair<TKey, TValue>(key, value);\n                    return;\n                }\n            }","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Design/Collections/HybridDictionary.cs#L272-L308","documentation":"The Fetch method (backing the indexer getter) throws KeyNotFoundException when the requested key does not exist in the HybridDictionary. In list mode it scans stored pairs and throws if no match is found; in dictionary mode the underlying Dictionary indexer throws the same message. This is standard read-miss semantics.","triggerScenarios":"Reading `dict[key]` (or calling Fetch) with a key that was never added or was removed, in either list mode or dictionary mode.","commonSituations":"Typo'd or case-mismatched string keys with a case-sensitive comparer, reading before data is loaded, key removed by another part of the pipeline, races in multithreaded access.","solutions":["Use TryGetValue(key, out var value) instead of the indexer","Check ContainsKey before reading","Verify key spelling/casing against the comparer used at construction"],"exampleFix":"// before\nvar v = dict[\"missingKey\"]; // throws\n// after\nif (dict.TryGetValue(\"missingKey\", out var v)) { /* use v */ }","handlingStrategy":"try-catch","validationCode":"if (dict.TryGetValue(key, out var value)) { /* use value */ }","typeGuard":null,"tryCatchPattern":"try { var v = dict[key]; }\ncatch (KeyNotFoundException) { /* handle missing key */ }","preventionTips":["Always prefer TryGetValue over the indexer for possibly-missing keys","Verify key casing/spelling against the comparer used at construction"],"tags":["csharp","collections"],"backgroundTag":"resource-not-found","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}