Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

Selecting a Collection Class 

 

The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

 

FIFO

·        Queue class

·        Queue generic class

·        where the element is typically discarded after its value is retrieved

 

LIFO

 

sequential access

 

random access

 

Will each element contain one value, a combination of one key and one value, or a combination of one key and multiple values?

  • One value: Use any of the collections based on the IList interface or the IList generic interface.
  • One key and one value: Use any of the collections based on the IDictionary interface or the IDictionary generic interface.
  • One value with embedded key: Use the KeyedCollection generic class.
  • One key and multiple values: Use the NameValueCollection class.

 

Do you need to sort the elements differently from how they were entered?

  • The Hashtable class sorts its elements by their hash codes.
  • The SortedList class and the SortedDictionary and SortedList generic classes sort their elements by the key, based on implementations of the IComparer interface and the IComparer generic interface.
  • ArrayList provides a Sort method that takes an IComparer implementation as a parameter. Its generic counterpart, the List generic class, provides a Sort method that takes an implementation of the IComparer generic interface as a parameter.

 

Do you need fast searches and retrieval of information?

  • ListDictionary is faster than Hashtable for small collections (10 items or fewer). The SortedDictionary generic class provides faster lookup than the Dictionary generic class.

 

Do you need collections that accept only strings?

·         StringCollection (based on IList) and StringDictionary (based on IDictionary) are in the System.Collections.Specialized namespace.

·         In addition, you can use any of the generic collection classes in the System.Collections.Generic namespace as strongly typed string collections by specifying the String class for their generic type arguments.

 

 

When to Use Generic Collections 

Using generic collections is generally recommended, because you gain the immediate benefit of type safety without having to derive from a base collection type and implement type-specific members. In addition, generic collection types generally perform better than the corresponding nongeneric collection types (and better than types derived from nongeneric base collection types) when the collection elements are value types, because with generics there is no need to box the elements.

The following generic types correspond to existing collection types:

·                       List is the generic class corresponding to ArrayList.

·                       Dictionary is the generic class corresponding to Hashtable.

·                       Collection is the generic class corresponding to CollectionBase. Collection can be used as a base class, but unlike CollectionBase it is not abstract, making it much easier to use.

·                       ReadOnlyCollection is the generic class corresponding to ReadOnlyCollectionBase. ReadOnlyCollection is not abstract, and has a constructor that makes it easy to expose an existing List as a read-only collection.

·                       The Queue, Stack, and SortedList generic classes correspond to the respective nongeneric classes with the same names.

 

Additional Types

There are several generic collection types that do not have nongeneric counterparts:

·                       LinkedList is a general-purpose linked list that provides O(1) insertion and removal operations.

·                       SortedDictionary is a sorted dictionary with O(log n) insertion and retrieval operations, making it a useful alternative to SortedList.

·                       KeyedCollection is a hybrid between a list and a dictionary, which provides a way to store objects that contain their own keys.

 

 

 

Posted on Saturday, August 5, 2006 3:41 PM Collections | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: Selecting a Collection Class

# re: Selecting a Collection Class
Requesting Gravatar...
"SortedDictionary is a sorted dictionary with O(log n) insertion and retrieval operations"
"Dictionary is the generic class corresponding to Hashtable."
"The SortedDictionary generic class provides faster lookup than the Dictionary generic class."
I'm puzzeled about the last statement. Should the lookup of Dictionary not be O(1) as it is a Hashtable so that the Dictionary is faster than the SortedDictionary for lookup?
Left by Nere on Sep 13, 2007 9:14 PM

# re: Selecting a Collection Class
Requesting Gravatar...
Thanks for the idea.
Left by Kerja Dari Rumah on Nov 26, 2010 6:39 AM

# re: Selecting a Collection Class
Requesting Gravatar...
Its been very helpful to browsers. At least it is more easier to use.social media marketing strategies | Facebook advertising
Left by Kelly on Dec 21, 2011 5:00 PM

# re: Selecting a Collection Class
Requesting Gravatar...
My history with the Halo franchise has been complicated. My first contact with it was not with the 6.0 inch Android Phone
Left by weng on Nov 02, 2012 2:55 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net