Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Order in a .NET Generic Dictionary

Tags:

c#

generics

I have a class that inherits from a generic dictionary as follows:

Class myClass : System.Collections.Generic.Dictionary<int, Object>

I have added a list of values to this in a particular order, but I now wish to change that order. Is there any way (without removing and re-adding) that I could effectively re-index the values; so change the object at index 1 to now be at index 10 for example? For example, this doesn't work:

myClass[1].Index = 10;
like image 466
Paul Michaels Avatar asked Dec 05 '25 04:12

Paul Michaels


1 Answers

Have a look at the SortedDictionary class where you can sort your items by the key.

Normally, you cannot change the index of items in Dictionary because it is against the principle ;-)

like image 119
rochal Avatar answered Dec 07 '25 18:12

rochal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!