Hello Friends! In this blog, I’m going to give a brief introduction about the Collections in C#. Let’s discuss the below-mentioned topics with an example. By the end of this blog, you will be able to answer any basic questions related to collections.
1.Why we need collections?
In any language, once an array is declared we can never change the size of an array.
int arr = new int
Now, if you want to increase the size of an array what you are required to do.
- We can manually create a new array and copy the value of an old array into a new one.
- Array provides a method called Resize()
Whenever you execute Resize() at that time Resize() will create a new array with a given size and after that copy an old array value to the new one.
- things not possible with an array
- Increase the size of an array
- Inserting a value into the middle
- Deleting or Removing value from the middle
So is it possible with an array?
To overcome this problem we need collections.
2. What are collections?
Collections are called Dynamic Array. Collections are also like an array capable to store multiple values but have all 3 functionality and features available in collections.
Collections are Auto Resizing, which is automatically increased whenever required and add the value in collections, possible to insert values into the middle and delete or remove values from the middle.
All these functions are available for use in the collections.
3. Types of collections
There are 2 types of Collections in C#
- Non-Generic Collections
- Generic Collections
i.e. Stack, Queue, LinkedList, SortedList, ArrayList, Hashtable.
If you use the stack in any other language like (C or C++) we have a problem. Stack is not available directly, if you want to use the stack, first you have to define the stack then you can use the stack the same as the queue.
The main advantage of using .NET, all of the above data structures implemented and given in the form of collection classes.
Collection classes are available under a namespace called “System.Collections”.
Here, In the given example how collections are auto-resized.
static void Main(string args)
ArrayList myArray = new ArrayList();
Console.WriteLine("Capacity of ArrayList When Empty :" + myArray.Capacity);
Console.WriteLine("Capacity of ArrayList When 1 Element :" + myArray.Capacity);
Console.WriteLine("Capacity of ArrayList When 4 Element :" + myArray.Capacity);
Console.WriteLine("Capacity of ArrayList When 5 Element :" + myArray.Capacity);
Capacity Of ArrayList When Empty :0
Capacity Of ArrayList When 1 Element :4
Capacity Of ArrayList When 4 Element :4
Capacity Of ArrayList When 5 Element : 8
Here you can see our ArrayList is Empty that time capacity of ArrayList is 0 but after added 1st element capacity became 4 it is allocating the room 4 items at a time and once 4 items are filled it will become 8 and 8 will become 16 when we add 9th item it means it will double the capacity automatically.
If you pass initial capacity 5 in my collections
ArrayList myArray = new ArrayList(5)
It will start storing one by one item and this 5(Initial Capacity) became 10 when we add the 6th item. Now also it is the same behavior it means it will double. The only difference is if you don’t specify initial capacity then start with 0 after that 4 and double.
Here, we take examples of the most frequently used methods in collections.
|Add(object value)||Add uses for add values in our collections and store them in object type.
|Insert(int index,obj value)||Insert is used for insert value at the specific position we need to pass Index and Value.
|Remove(object obj)||Remove the specific objects from collections.|
|RemoveAt(int index)||RemoveAt is to remove the object from a given index.|
|Contains(object item)||Check given item is present or not in our collections.|
2. Generic collections
i.e. List<T>, Dictionary<TKey,TValue>, Queue<T>, Stack<T>, SortedList<TKey,TValue>
A generic collection strong typed you can store one type of object into it. So, we can improve the performance by avoiding boxing and unboxing.
All generic collections are available under a namespace called “System.Collections.Generic”.
Here stack and queue are in both types of collections generic and non-generic so the difference is when we use non-generic stack or queue that time we no need to define a data type, but when we use generic stack at that time we must be defined which type of data entered into our stack or queue.
Syntax of how to declare generic collections.
List<T> myList = new List<T>();
Here, T = data type
So when we declare a generic collection class that time we need to specify a data type.
Here, we take examples of the most frequently used methods in the generic collections.
|Add(T item)||Add uses for add items in our collections and store in T type.|
|Insert(int index,T item)||Insert is used for insert value at the specific position we need to pass Index and Item.|
|Remove(T item)||Remove specific items from collections.|
|RemoveAt(int index)||RemoveAt is to remove an item from a given index.|
|Contains(T item)||Check given item is present or not in our collections.|
So the difference between methods is only when we will use generic collections at that time we need to define a type which type of data we want to insert in our collection class and when we will try to insert another type of data at that time it will generate a compile-time error.
4. Difference between types of collections
|Namespace : |
|Hold elements of any data type.||Hold only the defined type of data.|
|No need to |
specify data type at declaration time.
data type at declaration time.
|Weakly typed and not Typesafe.||Strongly typed and Typesafe.|
|Checked for type |
safety at Runtime.
|Checked for type
safety at Compiletime.
In this blog, we covered what are collections, why we need collections, types of collections, how to use methods in collections, and at last, we discussed the difference between types of collections. So, I hope now you have a clear idea about the basics of collections.