2

Say that, in my method, I pass in a couple IEnumerables (probably because I'm going to get a bunch of objects from a db or something).

Then for each object in objects1, I want to pull out a diffobject from objects2 that has the same object.iD.

I don't want multiple enumerations (according to resharper) so I could make objects2 into a dictionary keyed with object.iD. Then I only enumerate once for each. (secondary question)Is that a good pattern?

(primary question) What's too big? At what point would this be a horrible pattern? How many objects is too many objects for the dictionary?

shawn.mek
  • 1,195
  • 1
  • 11
  • 17
  • 1
    Perhaps give it a try and find out if it's really a problem before trying to optimize something that doesn't exist yet? – Bryan Crosby Jun 04 '12 at 22:30

2 Answers2

3

Internally, it would be prevented from ever having more than two billion items. Since the way things are positioned within a dictionary is fairly complicated, if I were looking at dealing with a billion items (if a 16-bit value, for example, then 2GB), I'd be looking to store them in a database and retrieve them using data-access code.

I have to ask though, where are Objects1 and Objects2 coming from? It sounds as though you could do this at the DB level and it would be MUCH, MUCH more efficient than doing it in C#!

You might also want to consider using KeyValuePair[]

Faraday
  • 2,904
  • 3
  • 23
  • 46
0

Dictionaries store instances of KeyValuePair

If all you ever want to do is look up values in the dictionary given their Key, then yes, Dictionary is the way to go - they're pretty quick at doing that. However, if you want to sort items or search for them using the Value or a property of it, it's better to use something else.

As far as the size goes, they get a little slower as they get bigger, it's worth doing some benchmarks to see how it affects your needs, but you could always split values across multiple dictionaries based on their type or range. http://www.dotnetperls.com/dictionary-size

It's worth noting though that when you say "Then I only enumerate once for each", that's slightly incorrect. objects1 will be enumerated fully, but the dictionary of objects2 won't be enumerated. As long as you use the Key to retrieve values, it will hash the key and use the result to calculate a location to store the value, so a dictionary can get pretty quickly to the value you ask for. Ideally use an int for the Key because it can use that as the hash directly. You can enumerate them, but it's must better to look objects up using objects2Dictionary[key].

Community
  • 1
  • 1
greg84
  • 7,541
  • 3
  • 36
  • 45