stride3d/stride · error · InvalidOperationException
Unable to set this value at the given index because this…
Error message
Unable to set this value at the given index because this value is already contained in this ObservableSet.
What it means
ObservableSet enforces set semantics: it keeps a parallel HashSet to guarantee uniqueness. SetItem (indexer) removes the old item from the hash set and tries to add the new one; if the new item is already present elsewhere in the set, it would create a duplicate, so the old item is restored and InvalidOperationException is thrown.
Solutions
- Check Contains(newItem) before index assignment and remove the duplicate first
- Use Add/Remove semantics instead of indexer assignment when values may collide
- If the intent is replacement, Remove(existing equal item) then assign
- Verify your equality/comparer semantics — the 'duplicate' may be unintended
Example fix
// before
set[0] = existingItem; // throws if already contained
// after
if (!set.Contains(existingItem))
set[0] = existingItem;
else
set.Remove(set[0]); // or handle duplicate explicitly Defensive patterns
Strategy: validation
Validate before calling
if (!observableSet.Contains(newItem)) observableSet[index] = newItem;
Try / catch
try { set[i] = value; } catch (InvalidOperationException) { /* value already in set; remove duplicate first */ } Prevention
- Treat ObservableSet as a set, not a list — prefer Add/Remove over indexer assignment
- Check Contains before assigning
- Review your equality comparer for unintended collisions
When it happens
Trigger: Assigning via collection[index] = item where item already exists in the set (per equality comparer); replacing an item with one equal to a different existing element.
Common situations: Treating the set like a List and assigning values; equality comparer changes making items collide; updating an item to a value equal to another tracked item.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Unable to remove this value at the given index because it…
- An asset [ ] with the same location [ ] is already…
- An asset with the same id
- An element with the same key has already been added.
- An id is both marked as deleted and associated to a key of…
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/ce46e86c8f52c7d6.
Report an issue: GitHub.
Appendix: source
Thrown at sources/presentation/Stride.Core.Presentation/Collections/ObservableSet.cs:98
}
protected override void InsertItem(int index, T item)
{
if (hashSet.Add(item))
{
base.InsertItem(index, item);
}
}
protected override void SetItem(int index, T item)
{
var oldItem = base[index];
hashSet.Remove(oldItem);
if (!hashSet.Add(item))
{
// restore removed item
hashSet.Add(oldItem);
throw new InvalidOperationException("Unable to set this value at the given index because this value is already contained in this ObservableSet.");
}
base.SetItem(index, item);
}
protected override void RemoveItem(int index)
{
var item = base[index];
if (!hashSet.Remove(item))
{
// safety check: shouldn't happen. If it does, we have a failed logic somewhere.
throw new InvalidOperationException("Unable to remove this value at the given index because it wasn't found in the ObservableSet.");
}
base.RemoveItem(index);
}
/// <inheritdoc/>
[CollectionAccess(CollectionAccessType.None)]
public override string ToString()View on GitHub (pinned to 96fad776d2)