Tuesday, 8 January 2019

Top 5 C# Collections that Every C# Developer Must Know

Top 5 C# Collections that Every C# Developer Must Know

Collections provide a flexible way to working with a group of objects. It is used for storage and retrieval of data.
Collections allocate memory dynamically to store data and then retrieve stored data using key or index when required in your application.

Collections are not needed to define the size of the collection. You can add or remove data at any time at any point from the collection.

There are 2 types of collections in C#
  • Non Generic Collections
  • Generic Collections


For non-generic collections, you need to use System.Collections namespace that includes the interfaces and classes.

IEnumerator, IEnumerable, and ICollection are the top level interfaces for all the collections in C#.



The ICollection interface is the base interface for all non-generic collections.
The Queue and Stack collection implement ICollection inferface

The ArrayList and BitArray collections implement IList interface that includes methods and properties to add, insert, remove, clear, copy elements in the collection and element can be retrieved by index.

The Hashtable and SortedList implement IDictionary interface that represents a non-generic collection of key/value pairs. so they store key/value pairs.

Following are the non-generic collections

Hashtable:-  It implement IDictionary interface. It store data as key/value pairs and retrieve data with comparing the hash value of the keys.

SortedList:-  It implement IDictionary interface. It store data as key/value pairs and default it automatically sort the date ascending order of keys values.

ArrayList: - It implement IList interface. It stores any type of data and not need to define the size of it.

BitArray: - It implement IList interface and store the Boolean value as true/false.

Queue: - It implements ICollection interface to store data as FIFO (first in first out) manner.

Stack: - It implement ICollection interface to store data as LIFO (last in first out) manner.

No comments:

Post a Comment