C# : Distinctions between various <string, string> Collections
Here's a question that I always come back too every so often:
What's the best <string, string>
Collection to use, for some xyz situation (most often to bind to a DropDownList
) ?
Does anyone have a cheat sheet for this? a comprehen开发者_StackOverflowsive list of distinctions between the various options?
If you're binding to a dropdown list, you probably have a sort order in mind and the size of the collection will (most likely and in most use cases) be small enough to avoid performance concerns. In these cases, a List<KeyValuePair<string, string>>
is a pretty easy choice, although a BindingList
might work better for binding, especially in WPF.
Tuple<string, string>
can replace KeyValuePair
even.
Furthermore, the non-generic (not strong typed) collections often give some of the worst performance with boxing (on top of being unwieldy to work with) and if you're worried about list overhead, you can specify a maximum size on creation to minimize that. Another advantage of the generic classes is that they implement IEnumerable
for use with Linq and, in my experience, tend to be more widely used and better known to your colleagues. In general, there should be 1 obvious way to do something in a language and the .Net community has chosen Dictionary<string, string>
over StringDictionary
.
You can also add extension methods to make basic lists more convenient:
public static class ListKeyValuePairExtensions
{
public static void Add<S, T>(this List<KeyValuePair<S, T>> list, S key, T value)
{
list.Add(new KeyValuePair<S, T>(key, value));
}
}
Edit: As pointed out by Porges, in cases addressed by this question, the performance hit of non-generic structures is not from boxing and unboxing, there is still a performance hit however, see this article for a quick benchmark.
Here's the extent of my knowledge on this:
StringDictionary strDict = new StringDictionary();
// + Strong Type
// - lowercases the key
// - random order ?
// - older approach, .Net 1 which predates generics
Dictionary<string, string> dict = new Dictionary<string, string>();
// + Strong Type
// - random order ?
List<KeyValuePair<string, string>> listKVP = new List<KeyValuePair<string, string>>();
// + Strong Type
// + Keeps order as inserted
// - more complex to instanciate and use
Hashtable hash = new Hashtable();
// Automatically sorted by hash code
// Better for big collections
// - not strong typed
ListDictionary listDict = new ListDictionary();
// + faster than Hashtable for small collections (smaller than 10)
// - not strong typed
HybridDictionary hybridDict = new HybridDictionary();
// Better compromise if unsure of length of collection
// - not strong typed
OrderedDictionary orderDict = new OrderedDictionary();
// + Keeps order as inserted
// - not strong typed
SortedDictionary<string, string> sortedDict = new SortedDictionary<string, string>();
// + Strong Type
// Automatically sorted by key
// + faster lookup than the Dictionary [msdn]
SortedList<string, string> sortedList = new SortedList<string, string>();
// + Strong Type
// Automatically sorted by key
// Almost same as SortedDict, but can access by index []
KeyValuePair<string, string>[] arrayKVP = new KeyValuePair<string, string>[123];
// + Strong Type
// + Keeps order as inserted
// - Fixed size
精彩评论