4

I am just searching on internet about the advantages and disadvantages of various collections of the .NET. I found the following points

  1. Dictionary is faster than List in the context of lookup for value.
  2. HashSet is faster than List in the context of lookup for value.
  3. Dictionary is faster than Hashtable in the context of lookup for value. None of them guarantee preserving the order of items.
  4. I read that Hashset is fasted collections in .NET

So I come to the following sorted order for the .NET Collections

  1. Hashset
  2. Dictionary
  3. Hashtable
  4. List
  5. ArrayList

For the above sorted order I have used the following links

And besides the above question I found some useful links which I want to share

Is the above sorted order is in correct order ? If not can you please rearrange it ? If anyone can add some more collections into the sorted order of the above list then it would be appreciated.

Community
  • 1
  • 1
Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124
  • 1
    `Hashtable` and `Dictionary` are more or less equivalent, the latter being generic, as explained in [Why Dictionary is preferred over hashtable in C#?](http://stackoverflow.com/questions/301371/why-dictionary-is-preferred-over-hashtable-in-c). `Dictionary` doesn't preserve the order of its elements: its [MSDN page](http://msdn.microsoft.com/en-us/library/xfhwa508.aspx) states that "The order in which the items are returned is undefined.". – Julien Lebosquain Aug 19 '11 at 09:23

3 Answers3

4

I think this article can be useful C#/.NET Fundamentals: Choosing the Right Collection Class

Incognito
  • 16,567
  • 9
  • 52
  • 74
2

It depends on how you perform the lookup. That's one of the reasons why there's so many different collections. Another would be the characteristics of insert operations.

all collections serve a specific purpose. If you have a lookup key then Dictionaray<Tkey,Telement> is faster than searching a List<T> or a Hashset<T> (unless the object is the key). If you have an index List<T> is faster than Dictionary and array is even faster.

If the lookup needs to find all objects that full fill a given requirement. E.g all ints in a collection of ints where 10

So there's no set order when it comes to lookup performance. It depends on the characteristics of the lookup.

And the lookup performance only tells part of the story. The set of characteristics must be analysed as a hole to find the collection for a given task.

  • Are you going to have a lot of inserts
  • Hows lookup performed
  • Is sorting required

are some that spring to mind

Rune FS
  • 21,497
  • 7
  • 62
  • 96
0

SortedDictionary can be added as well in the list as it preserves elements in the order .

Thanks

62071072SP
  • 1,963
  • 2
  • 19
  • 38