How are people dealing with set accessors on custom indexes with respect to overwriting values? For example, you have a customer collection of items that have Guids. You decide that an indexer this[someGuid] is a useful thing. But look at this example:
CustomerCollection customers = new CustomerCollection();
Customer cust1 = new Customer(); // internally, the ctor assigns customer a guid.
Guid g = cust1.Guid; // cache the guid in g.
customers.Add(cust1); // add our cust1 to the collection.
Customer c = customers[g]; // retrieving an object via the indexer is fine.
customers[g] = new Customer(); // but here's the dichotomy; the cust at g will be replaced by a cust with a new guid.
Depending upon the implementation of the indexer's set accessor, in that last line, the cust1 object could either be replaced by the new incoming customer (that would have a new guid), or it could be written so that the set accessor would only allow replacements by objects that have the same guid.
Or you could think of it this way;
son2.Name = "David"; sonsCollection["Mark"] = son2;
Son randomSon = sonsCollection["Mark"]; //runtime error - Mark's gone!
So which is the programmer's morally right way? We should code in the way which is the expected behaviour. Which is it? Personally, I vote for overwriting/replacing the value at the index, even if it does invalidate the index at which you're writing to.