Verbal Kint Verbal Kint - 1 year ago 86
C# Question

Difference between List<int> and Dictionary<int, int>?

Why would anyone ever use a dictionary with two integers?

It seems to me like this would just create an indexed collection of integers, where the "key" is the integer index and the "value" is the integer.

Or, in other words, it would be the same as a list of integers.

Now, I suppose having a dictionary allows you to set custom integer keys, so for example you could have pairs like (1,2) (7,3), etc. but this still doesn't make much sense. But when would it actually be practical/useful to use a dictionary as opposed to List?

Answer Source

For example, if you have a database-table with customers.

And you have another table with orders.

You want to get the number of different orders of every customer from the database.

Then you could save it as a Dictionary<int, int>, where the first int is the customer's id and the second int is the number of different orders they have.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download