Can dictionaries be used when I don’t need to quickly access their meanings?

I usually use the dictionary as a list, but with a different type of key. I like the ability to quickly access individual elements in the dictionary without having to scroll until I find the element with the property right (because the property I'm looking for is in the key).

But there is another possible use of the dictionary. I could just use the key to store property A and Value to store property B without using the special dictionary functionality. For example, I could keep a list of people by simply storing the first name in the key and the last name in the value (let's say for simplicity that there will never be two people with the same name, because I just could not come up with a better example). I would use only this dictionary to iterate over it in the foreach loop and add elements to it (without deleting, sorting, or accessing individual elements). In fact, there is no difference in using List<KeyValuePair<string, string>> from using Dictionary<string, string> (at least not in the example I gave - I know that I could, for example, store several elements with with the same key in the list).

So, to summarize, what should I do when I do not need to use the special functions that the dictionary offers, and just use it to store something that has exactly two properties:

  • use Dictionary<,>
  • use List<KeyValuePair<,>
  • use List<MyType> with MyType as a custom class that contains two properties and a constructor.
+7
dictionary c #
source share
3 answers

Do not use dictionaries for this.

If you do not want to create a class for this purpose, use something like List<Tuple<T1,T2>> . But keep in mind that a custom class will be more readable and more flexible.

Here's the reason: reading code is best if you use the right data structures. Using the dictionary only confuses the reader, and you will have problems the day the duplicate key appears.

If someone reads your code and sees that Dictionary is being used, he will assume that you really want to use a structure similar to a map. Your code should be clear, and your intentions should be obvious when reading.

+19
source share

If you are interested in performance, you should probably store data in a List . A Dictionary has a lot of internal overhead. Both memory and processor.

If you are only interested in readability , choose the data structure that best reflects your intentions. If you store key-value pairs (for example, custom fields in an error tracking error), use Dictionary . If you just store items without using your logical key, use List .

It takes a little work to create a custom class to use as an element in a List . Using Dictionary just because it gives you the Key property for each element is a misuse of this data structure. It is easy to create a custom class that also has a Key property.

+6
source share

Use List<MyType> , where MyType includes all values.

The problem with the vocabulary approach is that it is not flexible. If you later decide to add middle names, you will need to reverse engineer the entire data structure, and not just add another field to MyType.

+2
source share

All Articles