C Sharp Collections

C# Collections

A group of objects is represented by a collection in C#, which is also used to perform different operations on objects which are listed below:

  • store object
  • update object
  • delete object
  • retrieve object
  • search object, and
  • sort object

Thus the C# collections perform all the data structure work. Just like an array, a collection can also be used to store objects with an advantage over an array. In a collection, the stored object can grow or shrink dynamically, however, in an array, there is a size limit.

Types of Collections:

To work with the collections in C#, one can use any of the three namespaces that are listed below:

  • System.Collections.Generic classes
  • System.Collections classes (Now deprecated)
  • System.Collections.Concurrent classes

System.Collections.Generic classes:

The classes of the System.Collections.Generic namespace are listed below:

  • List
  • Stack
  • Queue
  • LinkedList
  • HashSet
  • SortedSet
  • Dictionary
  • SortedDictionary
  • SortedList

System.Collections classes:

The classes of the System.Collections namespace are legacy. The System.Collections.Generic classes are now recommended to be used. The classes of the System.Collections namespace are listed below:

  • ArrayList
  • Stack
  • Queue
  • Hashtable

System.Collections.Concurrent classes:

The classes for thread-safe operations are facilitated by the System.Collections.Concurrent namespace. Thus, while accessing the collection items, the multiple threads do not create a problem. The classes of the System.Collections.Concurrent namespace are listed below:

  • BlockingCollection
  • ConcurrentBag
  • ConcurrentStack
  • ConcurrentQueue
  • ConcurrentDictionary
  • Partitioner
  • Partitioner
  • OrderablePartitioner