Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change cardinality of item in C# dictionary

I've got a dictionary, something like

Dictionary<Foo,String> fooDict

I step through everything in the dictionary, e.g.

foreach (Foo foo in fooDict.Keys)
    MessageBox.show(fooDict[foo]);

It does that in the order the foos were added to the dictionary, so the first item added is the first foo returned.

How can I change the cardinality so that, for example, the third foo added will be the second foo returned? In other words, I want to change its "index."

like image 666
Asmor Avatar asked Dec 14 '22 06:12

Asmor


1 Answers

If you read the documentation on MSDN you'll see this:

"The order in which the items are returned is undefined."

You can't gaurantee the order, because a Dictionary is not a list or an array. It's meant to look up a value by the key, and any ability to iterate values is just a convenience but the order is not behavior you should depend on.

like image 144
CodeRedick Avatar answered Dec 28 '22 08:12

CodeRedick