Is there such a datatype? Dictionaries throw exceptions, and lists allow multiple copies of the same thing.
Alternatively, if is there is a way to reduce the cost of making a .Union call on lists, that would accomplish the same thing in my case.
Is there such a datatype? Dictionaries throw exceptions, and lists allow multiple copies of the same thing.
Alternatively, if is there is a way to reduce the cost of making a .Union call on lists, that would accomplish the same thing in my case.
Dictionary
will only throw an exception if you call Add
. You can achieve your desired behavior by just using the indexer to assign.
Dictionary<string, int> dict = new Dictionary<string, int>();
dict["foo"] = 1;
dict["foo"] = 2;
Setting a value via the Item property allows duplicates.
Overwriting is bad in general, unless you wish to do so!
You can skip the duplicates by using HashSet.
We do this by subclassing System.Collections.ObjectModel.KeyedCollection and either shadowing (sorry, vb term) the Add method or overriding the Insert method depending on our needs.