Verbal Kint Verbal Kint - 2 months ago 7x
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?


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.