{"record":{"id":"ce46e86c8f52c7d6","repo":"stride3d/stride","slug":"unable-to-set-this-value-at-the-given-index-because-this","errorCode":null,"errorMessage":"Unable to set this value at the given index because this value is already contained in this ObservableSet.","messagePattern":"Unable to set this value at the given index because this value is already contained in this ObservableSet\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation/Collections/ObservableSet.cs","lineNumber":98,"sourceCode":"        }\n\n        protected override void InsertItem(int index, T item)\n        {\n            if (hashSet.Add(item))\n            {\n                base.InsertItem(index, item);\n            }\n        }\n\n        protected override void SetItem(int index, T item)\n        {\n            var oldItem = base[index];\n            hashSet.Remove(oldItem);\n            if (!hashSet.Add(item))\n            {\n                // restore removed item\n                hashSet.Add(oldItem);\n                throw new InvalidOperationException(\"Unable to set this value at the given index because this value is already contained in this ObservableSet.\");\n            }\n            base.SetItem(index, item);\n        }\n\n        protected override void RemoveItem(int index)\n        {\n            var item = base[index];\n            if (!hashSet.Remove(item))\n            {\n                // safety check: shouldn't happen. If it does, we have a failed logic somewhere.\n                throw new InvalidOperationException(\"Unable to remove this value at the given index because it wasn't found in the ObservableSet.\");\n            }\n            base.RemoveItem(index);\n        }\n\n        /// <inheritdoc/>\n        [CollectionAccess(CollectionAccessType.None)]\n        public override string ToString()","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation/Collections/ObservableSet.cs#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nset[0] = existingItem; // throws if already contained\n// after\nif (!set.Contains(existingItem))\n    set[0] = existingItem;\nelse\n    set.Remove(set[0]); // or handle duplicate explicitly","handlingStrategy":"validation","validationCode":"if (!observableSet.Contains(newItem)) observableSet[index] = newItem;","typeGuard":null,"tryCatchPattern":"try { set[i] = value; } catch (InvalidOperationException) { /* value already in set; remove duplicate first */ }","preventionTips":["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"],"tags":["collections","set","duplicate"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}