Search code examples
c#collectionsc5

How does Update work in C5 collection?


How do I use an update method in C5 collection?

For example, lets say I have a set where I want to replace item A with B. I'd expect it to be something like:

HashSet<String> s = new HashSet<String>();
s.add("A");
s.update("A", "B");

but instead, Update takes a single parameter, and the documentation has the following to say:

bool Update(T x) returns true if the collection contains an item equal to x, in which case that item is replaced by x; otherwise returns false without modifying the collection. If any item was updated, and the collection has set semantics or DuplicatesByCounting is false, then only one copy of x is updated; but if the collection has bag semantics and DuplicatesByCounting is true, then all copies of the old item are updated. If any item was updated, then events ItemsRemoved, ItemsAdded and CollectionChanged are raised. Throws Read- OnlyCollectionException if the collection is read-only.

Any ideas? Thanks.


Solution

  • I think you need to do this with two separate operations:

    s.Remove("A");
    s.Add("B");
    

    The Update method only works if the two items are considered equal (two different objects can be equal). But "A" and "B" are not equal.