C# associative array

后端 未结 10 2145
时光取名叫无心
时光取名叫无心 2020-12-11 00:12

I\'ve been using a Hashtable, but by nature, hashtables are not ordered, and I need to keep everything in order as I add them (because I want to pull them out in the same or

相关标签:
10条回答
  • 2020-12-11 01:00

    I believe that .NET has the OrderedDictionary class to deal with this. It is not generic, but it can serve as a decent Hashtable substitute - if you don't care about strict type safety.

    I've written a generic wrapper around this class, which I would be willing to share.

    http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx

    0 讨论(0)
  • 2020-12-11 01:01

    EDIT: LBushkin is right - OrderedDictionary looks like it does the trick, albeit in a non-generic way. It's funny how many specialized collections there are which don't have generic equivalents :( (It would make sense for Malfist to change the accepted answer to LBushkin's.)

    (I thought that...) .NET doesn't have anything built-in to do this.

    Basically you'll need to keep a List<string> as well as a Dictionary<string,FreeTextboxControl>. When you add to the dictionary, add the key to the list. Then you can iterate through the list and find the keys in insertion order. You'll need to be careful when you remove or replace items though.

    0 讨论(0)
  • 2020-12-11 01:01

    use sorted list i think it will solve your problem becuase SortedList object internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key/value pair that can be accessed as a DictionaryEntry object

    SortedList sl = new SortedList();

    foreach(DictionaryEntry x in sl) {}

    0 讨论(0)
  • 2020-12-11 01:02

    See Indexers: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

    0 讨论(0)
提交回复
热议问题