{"record":{"id":"00d80a48ee438f39","repo":"stride3d/stride","slug":"an-item-with-the-same-key-has-already-been-added","errorCode":null,"errorMessage":"An item with the same key has already been added.","messagePattern":"An item with the same key has already been added\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Design/Collections/HybridDictionary.cs","lineNumber":125,"sourceCode":"                return valueCollection;\n            }\n            return valueCollection = dictionary!.Values;\n        }\n    }\n\n    public int Count => list?.Count ?? dictionary!.Count;\n\n    public bool IsReadOnly => false;\n\n    public void Add(KeyValuePair<TKey, TValue> item)\n    {\n        CheckInvariant();\n        if (list != null)\n        {\n            foreach (var kvp in list)\n            {\n                if (keyComparer.Equals(kvp.Key, item.Key))\n                    throw new ArgumentException(\"An item with the same key has already been added.\");\n            }\n        }\n        InternalAdd(item);\n    }\n\n    public void Add(TKey key, TValue value)\n    {\n        CheckInvariant();\n        if (list != null)\n        {\n            foreach (var kvp in list)\n            {\n                if (keyComparer.Equals(kvp.Key, key))\n                    throw new ArgumentException(\"An item with the same key has already been added.\");\n            }\n        }\n        InternalAdd(new KeyValuePair<TKey, TValue>(key, value));\n    }","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Design/Collections/HybridDictionary.cs#L107-L143","documentation":"HybridDictionary.Add(KeyValuePair) throws ArgumentException when an entry with the same key already exists in the collection. While the collection is in list mode it does a linear scan with the key comparer; after cutover to Dictionary the same rule is enforced by Dictionary.Add. This mirrors IDictionary<TKey,TValue> semantics where Add does not overwrite.","triggerScenarios":"Calling Add(kvp) (or the IDictionary Add(object,object) path) with a key that was already inserted, when the collection still stores items in its internal list.","commonSituations":"Re-processing the same input records and re-adding them, registering the same name/component twice, loading data with duplicate keys.","solutions":["Check ContainsKey(key) before calling Add","Use the indexer `dict[key] = value` or TryAdd semantics if overwrite is intended","Deduplicate the source data before inserting"],"exampleFix":"// before\ndictionary.Add(new KeyValuePair<string,int>(\"x\", 1));\ndictionary.Add(new KeyValuePair<string,int>(\"x\", 2)); // throws\n// after\nif (!dictionary.ContainsKey(\"x\")) dictionary.Add(new KeyValuePair<string,int>(\"x\", 2));","handlingStrategy":"validation","validationCode":"if (dictionary.ContainsKey(item.Key))\n    dictionary[item.Key] = item.Value; // or skip\nelse\n    dictionary.Add(item);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer indexer assignment or TryAdd when overwrite semantics are acceptable","Deduplicate input collections before bulk insertion"],"tags":["csharp","collections"],"backgroundTag":"invalid-argument-value","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"}