The objective of this article is to understand the concept of collections in C# programming language. Predominantly, collections (types) are used to manipulate and manage a group of similar types of data. As we know, Arrays are also used to manage a similar type of data, but collections provide more flexibility when working with groups of objects. In the subsequent section of this article, we will help you to understand various types of collections with practical examples.
What are Collections in C#?
The collection is nothing but a group of similar types of objects on which we can perform the various operations like insert, delete, update, sorting, etc. unlike arrays, collections provide a more efficient way to manage the group of objects. Basically, there are two types of collections: generic and non-generic collections. This article only covers non-generic collections. If you want to learn about generic collections, then refer to our next article.
The non-generic collections based on System.Collections namespace. This type of collection has classes like ArrayList, Hashtable, Stack, and Queue. Before using any class, we must declare an instance of that class. Let’s take the example of each collection one by one.
1. ArrayList
ArrayList is an ordered collection of objects based on an index. Unlike an array, we don’t need to specify the size of ArrayList. We can add any type of element into an ArrayList like int, string, float, etc. using add() method.
Let's see the implementation of ArrayList.
Filename: Program.cs
using System;
using System.Collections;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(50);
al.Add(40);
al.Add(30);
al.Add(20);
al.Add(10);
Console.WriteLine("Number of elements: "+al.Count);
Console.Write("Elements: ");
foreach(int i in al)
{
Console.Write(i+" ");
}
al.Sort();
Console.Write("\nSorted Elements: ");
foreach(int i in al)
{
Console.Write(i+" ");
}
}
}
}
Output:
Elements: 50 40 30 20 10
Sorted Elements: 10 20 30 40 50
In the above example, Firstly, we have defined a System.Collections namespace to use the ArrayList class, then we created an object of the same class and using add() method we have added the elements. Once all the elements added, we used Count property to get the total count of elements present in the ArrayList and to printed all the elements using a foreach. Finally, the Sort() method arranges the elements in ascending order and the same foreach loop used to print all the elements.
2. Hashtable
A hash table is a special type of collection used to store key-value pairs. This collection is similar to generic Dictionary collection. The arrangement of hashtable key-value pairs is based on the hash code of the key. We are using the same add() method to add the elements in key-value pairs.
Let's see an implementation of Hashtable
Filename: Program.cs
using System;
using System.Collections;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(50,"Mangoes");
ht.Add(20,"Oranges");
ht.Add(12,"Bananas");
foreach(int key in ht.Keys)
{
Console.WriteLine("Key: {0}, Value: {1}",key,ht[key]);
}
}
}
}
Output:
Key: 20, Value: Oranges
Key: 12, Value: Bananas
Key: 50, Value: Mangoes
Code Explanation: In this example, we have created an object of Hashtable class, and with the help of the object and add() method, we have added key-value pairs. Key and value indicate the number of quantities and fruit names respectively. Lastly, we have printed all the keys and values using the foreach loop (ht is an object and Keys is the property name).
3. Stack
The stack is a linear data structure mainly based on the push and pop operations of the elements. The push operation adds an element in a collection, whereas pop operation used to remove the most recently added element from a collection. It follows the LIFO principle, which means Last In First Out. According to the LIFO principle, the last element pushed in the stack will get popped first.
Let's see an implementation of Stack
Filename: Program.cs
using System;
using System.Collections;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
Stack s = new Stack();
s.Push(10);
s.Push(20);
s.Push(30);
s.Push(40);
s.Push(50);
Console.WriteLine("Stack Elements: ");
foreach(int i in s)
{
Console.WriteLine(i+" ");
}
Console.WriteLine("Peek Element: "+s.Peek());
Console.WriteLine("Element Popped: "+s.Pop());
Console.WriteLine("\nStack Elements: ");
foreach(int i in s)
{
Console.WriteLine(i+" ");
}
}
}
}
Output:
Stack Elements:
50
40
30
20
10
Peek Element: 50
Element Popped: 50
Stack Elements:
40
30
20
10
In this example, Firstly, we have created an object of Stack class, and with the help of an object, we have called a push, pop, and peek methods. The push method adds the element, the pop method removes the last pushed element, and the peek method returns the top element of the stack. In our case, 10 is the first pushed element, and 50 pushed lastly. Hence the last element got popped first i.e 50.
4. Queue
The queue is a linear data structure mainly based on the enqueue and dequeue operations of the elements. Unlike stack, the queue is open at both ends (consider queue like a hollow pipe which has two ends). Similarly, the queue has two ends REAR and FRONT. The first element inserted from one end is called REAR and deleted from the other end called FRONT. The enqueue operation adds an element in a collection, whereas dequeue operation used to remove the firstly added element from a collection. It follows the FIFO principle, which means First In First Out. According to the FIFO principle, the first element inserted in the queue will get removed first.
Let’s see an implementation of Queue
Filename: Program.cs
using System;
using System.Collections;
namespace Studytonight
{
public class Program
{
public static void Main(string[] args)
{
Queue q = new Queue();
q.Enqueue(10);
q.Enqueue(20);
q.Enqueue(30);
q.Enqueue(40);
q.Enqueue(50);
Console.WriteLine("Queue Elements: ");
foreach(int i in q)
{
Console.WriteLine(i+" ");
}
Console.WriteLine("Peek Element: "+q.Peek());
Console.WriteLine("Element Popped: "+q.Dequeue());
Console.WriteLine("\nQueue Elements: ");
foreach(int i in q)
{
Console.WriteLine(i+" ");
}
}
}
}
Output:
Queue Elements:
10
20
30
40
50
Peek Element: 10
Element Popped: 10
Queue Elements:
20
30
40
50
In this example, Firstly, we have created an object of Queue class, and with the help of an object, we have called enqueue, dequeue, and peek methods. The enqueue method insert the element, the dequeue method removes the first inserted element, and the peek method returns the head element of the queue. In our case, 10 is the first inserted element, and 50 inserted lastly. Hence the first element got removed first i.e 10.
We hope this article with practical examples of collections helped you to understand the concept. Refer to our next article on generic collections.
You may also like: