{"record":{"id":"fae7e1944802be04","repo":"stride3d/stride","slug":"element-already-exists","errorCode":null,"errorMessage":"element already exists","messagePattern":"element already exists","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":538,"sourceCode":"        ArgumentNullException.ThrowIfNull(key);\n\n        var table = this.table;\n\n        var freeIndx = -1;\n\n        try\n        {\n            freeIndx = Find(key);\n        }\n        catch (Exception)\n        {\n            throw new InvalidOperationException();\n        }\n\n        if (freeIndx >= 0)\n        {\n            if (!overwrite)\n                throw new ArgumentException(\"element already exists\");\n\n            table[freeIndx] = new KeyValuePair<TKey, TValue>(key, value);\n            ++modificationCount;\n            return;\n        }\n\n        freeIndx = ~freeIndx;\n\n        if (freeIndx > Capacity + 1)\n            throw new Exception(\"SortedList::internal error (\" + key + \", \" + value + \") at [\" + freeIndx + \"]\");\n\n\n        EnsureCapacity(Count + 1, freeIndx);\n\n        table = this.table;\n        table[freeIndx] = new KeyValuePair<TKey, TValue>(key, value);\n\n        ++inUse;","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L520-L556","documentation":"PutImpl is the internal insert routine used by the constructor and Add(). When the binary search finds the key already present (freeIndx >= 0) and the overwrite flag is false, Add throws ArgumentException(\"element already exists\") because SortedList requires unique keys. This is the duplicate-key case of adding to a sorted collection.","triggerScenarios":"sortedList.Add(existingKey, someValue); building a SortedList from data with duplicate keys (e.g. a config file or CSV with repeated keys); re-running an initialization routine that re-adds the same keys.","commonSituations":"Loading key/value pairs from unordered or duplicated external data; double initialization of a lookup table; merging two collections that share keys.","solutions":["Use the indexer sortedList[key] = value to upsert (insert or overwrite).","Check ContainsKey(key) (or IndexOfKey(key) < 0) before calling Add.","Deduplicate the source data before populating the SortedList.","Remove the existing key first if replacement is intended."],"exampleFix":"// before\nsortedList.Add(\"timeout\", 30); // throws if exists\n// after\nsortedList[\"timeout\"] = 30; // upsert","handlingStrategy":"validation","validationCode":"if (sortedList.ContainsKey(key))\n    sortedList[key] = value;\nelse\n    sortedList.Add(key, value);","typeGuard":"static bool IsNewKey<TKey,TValue>(System.Collections.Generic.SortedList<TKey,TValue> list, TKey key) =>\n    !list.ContainsKey(key);","tryCatchPattern":"try { sortedList.Add(key, value); }\ncatch (ArgumentException ex) when (ex.Message == \"element already exists\")\n{ sortedList[key] = value; }","preventionTips":["Use the indexer for upsert semantics; use Add only for first-insert.","Deduplicate keys in source data before loading.","Make initialization idempotent: check ContainsKey before re-adding on retries.","When merging collections, decide a collision policy (overwrite, keep first, throw) up front."],"tags":["collections","duplicate-key","sortedlist"],"backgroundTag":"duplicate-key","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"}