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?
C# – Dictionary vs List: Key Differences
cdata structuresnet
Best Answer
You would use a
Dictionary<int, int>
if your indexes have a special meaning besides just positional placement.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
[person-id]
column and a[personal-pin]
column, then you might bring those into aDictionary<int, int>
. This waypinDict[person-id]
gives you a PIN, but the index is meaningful and not just a position in aList<int>
.But really, any time you have two related lists of integers, this could be an appropriate data structure.