Collections in C#

In c#, the collection is a class that is useful to manage a group of objects in a flexible manner to perform various operations like insert, update, delete, get, etc., on the object items in a dynamic manner based on our requirements.

 

Generally, while working in c# applications, we will need to create or manage a group of related objects. In that case, we have two ways to create group objects in c#, i.e., using the arrays and collections.

 

In the previous section, we learned about arrays in c#, but those are useful only when working with a fixed number of strongly-typed objects. To solve this problem, Microsoft has introduced collections in c# to work with a group of objects that can grow or shrink dynamically based on our requirements.

 

In c#, the collection is a class. Hence, we need to declare an instance of the class before performing any operations like add, delete, etc., on the defined collection. The collections are implemented using the IEnumerable interface to access collection items using a foreach loop.

C# Collection Types

In c#, we have different types of collection classes available; those are

 

  • Non-Generic (System.Collections)
  • Generic (System.Collections.Generic)
  • Concurrent (System.Collections.Concurrent)

C# Non-Generic Collections

In c#, non-generic collection classes are useful to store elements of different data types, and these are provided by System.Collections namespace. These collection classes are legacy types, so whenever possible, try to use generic collections (System.Collections.Generic) or concurrent collections (System.Collections.Concurrent).

 

Following are the different types of non-generic collection classes provided by System.Collections namespace.

 

ClassDescription
ArrayList It is useful to represent an array of objects whose size is dynamically increased as required.
Queue It is useful to represent a FIFO (First In, First Out) collection of objects.
Stack It is useful to represent a LIFO (Last in, First Out) collection of objects.
Hashtable It is useful to represent a collection of key/value pairs organized based on the key's hash code.

C# Generic Collections

In c#, generic collections will enforce a type safety so we can store only the elements with the same data type, and these are provided by System.Collections.Generic namespace.

 

Following are the different types of generic collection classes provided by the System.Collections.Generic namespace.

 

ClassDescription
List It is useful to represent a list of objects that can be accessed by an index.
Queue It is useful to represent a FIFO (First In, First Out) collection of objects.
Stack It is useful to represent a LIFO (Last in, First Out) collection of objects.
SortedList<K,V> It is useful to represent a collection of key/value pairs sorted by a key.
Dictionary<K,V> It is useful to represent a collection of key/value pairs organized based on the key.

C# Concurrent Collections

In c#, concurrent collections are useful for accessing collection items from multiple threads, which are available from .NET Framework 4 with System.Collections.Concurrent namespace.

 

If we use multiple threads to access a collection concurrently, we need to use concurrent collections instead of non-generic and generic collections.

 

Following are the different types of concurrent collection classes provided by the System.Collections.Concurrent namespace.

 

ClassDescription
BlockingCollection It is useful to provide blocking and bounding capabilities for thread-safe collections.
ConcurrentBag It is useful to represent a thread-safe, unordered collection of objects.
ConcurrentDictionary<K,V> It is useful to represent a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
ConcurrentQueue It is useful to represent a thread-safe FIFO (First In, First Out) collection.
ConcurrentStack It is useful to represent a thread-safe LIFO (Last in, First Out) collection.
Partitioner It is useful to provide partitioning strategies for arrays, lists, enumerables.
OrderablePartitioner It is useful to provide a specific way of splitting an orderable data source into multiple partitions.