stride3d/stride · error · ArgumentException
An item with the same key has already been added.
Error message
An item with the same key has already been added.
What it means
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.
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
Example fix
// before
dictionary.Add(new KeyValuePair<string,int>("x", 1));
dictionary.Add(new KeyValuePair<string,int>("x", 2)); // throws
// after
if (!dictionary.ContainsKey("x")) dictionary.Add(new KeyValuePair<string,int>("x", 2)); Defensive patterns
Strategy: validation
Validate before calling
if (dictionary.ContainsKey(item.Key))
dictionary[item.Key] = item.Value; // or skip
else
dictionary.Add(item); Prevention
- Prefer indexer assignment or TryAdd when overwrite semantics are acceptable
- Deduplicate input collections before bulk insertion
When it happens
Trigger: 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.
Common situations: Re-processing the same input records and re-adding them, registering the same name/component twice, loading data with duplicate keys.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- This operation is not supported by the source tracker.
- The given key was not present in the dictionary.
- Internal error, the collection has already muted to a…
- The given item does not validate the collection constraint.
- Capacity must be greater than 0.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/00d80a48ee438f39.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Design/Collections/HybridDictionary.cs:125
return valueCollection;
}
return valueCollection = dictionary!.Values;
}
}
public int Count => list?.Count ?? dictionary!.Count;
public bool IsReadOnly => false;
public void Add(KeyValuePair<TKey, TValue> item)
{
CheckInvariant();
if (list != null)
{
foreach (var kvp in list)
{
if (keyComparer.Equals(kvp.Key, item.Key))
throw new ArgumentException("An item with the same key has already been added.");
}
}
InternalAdd(item);
}
public void Add(TKey key, TValue value)
{
CheckInvariant();
if (list != null)
{
foreach (var kvp in list)
{
if (keyComparer.Equals(kvp.Key, key))
throw new ArgumentException("An item with the same key has already been added.");
}
}
InternalAdd(new KeyValuePair<TKey, TValue>(key, value));
}View on GitHub (pinned to 96fad776d2)