28 December 2011

When to Use Generic Collections in C#

When to Use Generic Collections in C#

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 CollectionBaseCollection 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 QueueStack, and SortedList generic classes correspond to the respective nongeneric classes with the same names.
  • 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.

No comments:

Post a Comment

Comments Welcome