0

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.

soandos
  • 4,978
  • 13
  • 62
  • 96

4 Answers4

1

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;
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • But I just want to call .Add(1) and then .Add(1), not get an exception, and not have to call containsKey to check first. – soandos Nov 09 '11 at 04:55
  • 1
    Use the indexer instead of calling Add. There is no need to check to see if the key is already there. Take a look at my example. – Adam Robinson Nov 09 '11 at 04:56
  • But how do I know what index to "overwrite" (or not as the case may be)? – soandos Nov 09 '11 at 05:00
  • I'm not quite sure what you mean...the indexer takes he same key parameter as the Add function. You just pass the same key to the indexer. – Adam Robinson Nov 09 '11 at 05:02
  • I'm sorry, I'm not clear. My list takes one parameter in the add. the dictionary takes two. What should the key be? if I try to access a non-existent index, what happens? – soandos Nov 09 '11 at 05:16
  • @soandos: The key is your value that is potentially duplicated. I'm not understanding where the confusion comes from...you said "dictionaries throw exceptions", which led me to believe that you had (at least at one point) code that made use of a `Dictionary`, but you can't use it because it's throwing an exception. – Adam Robinson Nov 09 '11 at 13:01
1

Setting a value via the Item property allows duplicates.

SimonC
  • 6,590
  • 1
  • 23
  • 40
  • This is the same as @Adam Robinson's answer. The `[]` syntax is referred to as the `Item` property, or indexer. – SimonC Nov 09 '11 at 04:59
-1

Overwriting is bad in general, unless you wish to do so!

You can skip the duplicates by using HashSet.

Nayan
  • 3,092
  • 25
  • 34
-2

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.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • I downvoted because this is a bad solution. Shadowing (or hiding, in c# terminology) is a major red flag. In addition, creating a new collection type and overriding it's insert behavior is unnecessary since the library already exposes a type with the desired behavior. – Adam Robinson Nov 09 '11 at 05:06
  • Yes, but what about situations such as databinding where using the indexer is not an option? In this case, overriding Insert is the only option. – competent_tech Nov 09 '11 at 05:09
  • The question at hand doesn't seem to give any indication that this would be the case. – Adam Robinson Nov 09 '11 at 05:11
  • The question doesn't give any specific scenario. It is perfectly reasonable to assume, based on the wording, that the OP is looking for a generic solution. A generic solution should take into account numerous scenarios, including databinding. If the OP had asked for a specific scenario solution, I would agree with you. – competent_tech Nov 09 '11 at 05:19
  • I'm sorry, I simply disagree with you. The OP doesn't give any indication that his collection would be used in a binding context (he describes using `List`, for example, rather than something like `ObservableCollection` or `BindingList`, which would be more appropriate in a binding scenario). – Adam Robinson Nov 09 '11 at 13:03
  • And I still disagree with you. The OP clearly wants a collection construct that is keyed. Lists and ObservableCollections, as you know, are not keyed. A BindingList is an interface that is implemented by an underlying collection, so the problem still has to be resolved in that collection. There are many other scenarios to consider as well, such as when the collection is used in a library that is accessed by multiple apps. You don't want to have separate instructions for your consumers in each of these apps. My point is that yours is a very narrow solution while mine is a general solution. – competent_tech Nov 09 '11 at 16:12
  • I guess we'll just have to agree to disagree and let the votes speak for the community. For clarity's sake, `BindingList` is a collection class that implements `IBindingList`, which is the interface you're referring to. YAGNI. – Adam Robinson Nov 09 '11 at 16:37