Introduction to Collections in C#

 3606

Before diving into Collections, let’s first understand the disadvantages of using an array.

 

Arrays and their disadvantages:

Arrays can be used to store data of similar type. For example, if we are declaring an array of integer type then we can only store integer values in that array.

We have to provide the size of an array while declaring. We cannot modify the size at runtime.

Index of an array starts from 0.

If we try to delete or modify the array from the middle then we will need to shift all the elements to the left side to fill the empty location. We can do it easily for small sized arrays but for bigger size arrays it’s not affordable from the performance perspective.

To overcome above issues, we have collections. It was introduced in C# 1.0.

 

Collections:

Collections is having predefined classes which helps in providing different operations like sorting objects, searching objects, deleting objects and many more.

The predefined classes are present under System.Collections namespace.

In short we can call collections as dynamic array because of following features:

1.      We can increase the size of it dynamically

2.      We can insert or remove or update any element irrespective of its place

 

Types of Collections:

3 namespaces are available to work with collections. They are as follows:

1.      System.Collections.Generic 

2.      System.Collections (deprecated)

3.      System.Collections.Concurrent 

Let’s see the classes which comes under above namespaces.

 

1.System.Collections.Generic:

Following classes comes under System.Collections.Generic:

Ø  List

Ø  Stack

Ø  Queue

Ø  LinkedList

Ø  HashSet

Ø  SortedSet

Ø  Dictionary

Ø  SortedDictionary

Ø  SortedList

 

2.System.Collections:

As the classes under this namespace are deprecated. It is suggested to use the classes of the System.Collections.Generic namespace:

Ø  ArrayList

Ø  Stack

Ø  Queue

Ø  Hashtable

 

3.System.Collections.Concurrent:

It provides classes for thread safe operations:

Ø  BlockingCollection

Ø  ConcurrentBag

Ø  ConcurrentStack

Ø  ConcurrentQueue

Ø  ConcurrentDictionary

Ø  Partitioner

Ø  Partitioner

Ø  OrderablePartitioner

That’s about the Introduction to the Collections. In the upcoming articles, we will discuss more about the classes that comes under the 3 namespaces.

 

Love my work?

Consider buying me a coffee! Your support helps me continue creating content that you enjoy.


Tags:

CSharp
 

Post a Comment

Name
Email
Comment

*Be the first to comment