I'm looking for the most ideal data structure (for performance and ease of use) from which values can be retrieved by string key or index. Dictionary doesn't work because you can't really retrieve by index. Any ideas?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
You are looking for something like the SortedList class (here's the generic version as well).
One word of warning. The
OrderedDictionary
has really bad performance characteristics for most operations except insertion and lookup: Both removal and modification of a value may require a linear search of the whole list, resulting in runtime O(n). (For modification, this depends on whether access occurred by index or by key.)For most operations with reasonable amounts of data, this is completely inacceptable. Furthermore, the data structure stores elements both in a linear vector and in a hash table, resulting in some memory overhead.
If retrieval by index doesn't happen too often, a
SortedList
orSortedDictionary
will have much better performance characteristics (access by index can be achieved through theElementAt
extension method).If, on the other hand, access by index is the norm, then stop using dictionary data structures alltogether and simply store your values in a
List<KeyValuePair<TKey, TValue>>
. Although this means a linear search for access by key, all other operations are very cheap and overall performance is hard to beat in practice./EDIT: Of course, the latter is also a dictionary data structure in the theoretical sense. You could even encapsulate it in a class implementing the appropriate interface.
You want the OrderedDictionary class. You will need to include the System.Collections.Specialized namespace:
A Dictionary could work with linq. Although i dont know about possible performance issues. Dictionary.ElementAt(index);
I recommend using SortedDictionary<string, TValue> or SortedList<string, TValue>. Both have O(log n) search performance.
The differences are, as quoted from the MSDN library:
In my experience SortedDictionary is more adequate for most typical business scenarios, since the data is usually initially unsorted when using structures like this, and the memory overhead of SortedDictionary is seldom critical. But if performance is key for you, I suggest you implement both and do measurements.
There's System.Collections.ObjectModel.KeyedCollection< string,TItem>, which derives from Collection< TItem>. Retrieval is O(1).