So I ran into a Dictionary<int, int> today at work. This just seemed weird to me because I would have probably just used a List<int> instead. Is there a difference and would there be a use case where one structure would be preferred over the other?
|
|
|||
| show 1 more comment |
|
You would use a The immediate example that comes to mind is storing an id column and an int column in a database. For example, if you have a But really, any time you have two related lists of integers, this could be an appropriate data structure. |
|||||||||||
|
|
Think of the For example, say you wanted to store an association between a person's age and their height. You could use a
Not a very useful example, but the point is you wouldn't be able to do this as elegantly with a |
|||
|
|
|
Semantically, a Which one you will choose should depend on the range of the key values. If your keys in the If your key values are 100 |
||||
|
|
List<T>within the .NET framework is a random access array, where a lookup operation is typically faster than for aDictionary<int,T>. – Doc Brown Mar 10 '12 at 9:36