Collections in C#

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

 

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

 

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

 

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

C# Collection Types

In c#, we have a different type of collection classes are available, those are

 

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

C# Non-Generic Collections (System.Collections)

In c#, non-generic collection classes are useful to store an elements of different types and these are provided by System.Collections namespace. Now, 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 type of non-generic collection classes which are provided by System.Collections namespace.

 

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

C# Generic Collections (System.Collections.Generic)

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

 

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

 

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

C# Concurrent Collections (System.Collections.Concurrent)

In c#, concurrent collections are useful to access a collection items from multiple threads and these are available from .NET Framework 4 with System.Collections.Concurrent namespace.

 

In case, if we are using a multiple threads to access a collection concurrently, then we need to use concurrent collections instead of non-generic and generic collections.

 

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

 

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

PREVIOUS

C# Events
 

NEXT

C# Arraylist