In this article, we are going to discuss the collections in C#. The collection is used to manipulate the data like Inserting, sorting, and deleting.
Collection Functionality
- Adding or Inserting records
- Removing items
- Searching/Finding Items
- Replacing items
- Copy amd Clone of items
There are two types of Collections, Generic and Non- Generic Collections. Generic collection work with the generic data type. System.Collections
the namespace contains Non-generic collection and system.collections.generic
contains Generic collection.
Classes In Generic and Non-Generic
Generic | Non- Generic |
---|---|
List | ArrayList |
Dictionary | HashTable |
SortedList | SortedList |
Stack | Stack |
Queue | Queue |
Generic Collection
C# List
Generic List contains the specific type of elements like int, string. Size is not fixed so it grows automatically.
C# Dictionary
The dictionary contains key-value pairs.
C# SortedList
Sorted List stored the data in key-value pair in a specific order, it stores the data in ascending order of key by default.
C# Stack
Stack stores the generic data type in LIFO style(Last In First Out). It provides a Push() method to Add and Pop() used to retrieve the data.
C# Queue
Queue stores the generic data types in FIFO style(First In First Out). It provides the Enqueue() for insertion and Dequeue() for retrieve.
Non-Generic Collection
The non-generic collection is used to store the non-generic data types, it stores any type of data. We can use Non- Generic only when we are not sure about the data type of data.
C# ArrayList:
Array List is similar to List but the difference is that it can store any type of datatype value. The size of the array list is fixed and any number of elements can be stored.
C# HashTable:
HashTable stores the data in key-value pairs. It retrieves the values by comparing the key value.
C# SortedList:
SortedList stores the values in key-value pairs. By default, it arranges the key in ascending order. It stores any type of data (Generic or Non-Generic).
C# Stack
Stack stores the generic data type in LIFO style(Last In First Out). It provides a Push() method to Add and Pop() used to retrieve the data. Both generic and non-generic includes in Stack.
C# Queue
Queue stores the generic data types in FIFO style(First In First Out). It provides the Enqueue() for insertion and Dequeue() for retrieve.
Need help?
Read this post again, if you have any confusion or else add your questions in Community