In this article, we are going to talk about the queue, stack and hashtable collections in C#, how to use them and how to use the methods they provide.

If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.

To download the source code, you can visit Collections in C# Source Code. 

So, let’s start.

We are going to split this article into the following sections:

Queue Collection

The queue collection represents a first-in, first-out collection of objects. This means that we can place our objects in a queue collection in a certain order and remove those objects by the same order. So, the first object which goes in is the first object to go out.

To create an object instance of a queue collection we can use two different statements.

By using System.Collection.Generic namespace:

Queue<int> intCollection = new Queue<int>();

And by using System.Collection namespace:
Queue queueCollection = new Queue();

If we declare an object by providing a type (in our example an int), we can store only integer numbers inside. On the other hand, if we use the second example we can store different data types in a collection because it stores objects.

The Most Common Methods and Properties

The Enqueue method adds an element inside a collection:

Queue queueCollection = new Queue();

foreach (var item in queueCollection)

When we want to  remove an element at the beginning of the collection and return it, we are going to use the Dequeue method:
Queue queueCollection1 = new Queue();

int number = Convert.ToInt32(queueCollection1.Dequeue());
Console.WriteLine($"Removed element is: {number}");

foreach (var item in queueCollection1)

The Peek method returns the element at the beginning of the collection but does not remove it:
Queue queueCollection2 = new Queue();
int peekNumber = Convert.ToInt32(queueCollection2.Peek());
Console.WriteLine($"Returned element is: {number}");

foreach (var item in queueCollection2)

The Clear method removes all the elements from a collection.

If we want to check how many elements we have inside a collection, we can use the Count property:


Stack Collection

A stack collection represents a simple last-in, first-out collection. It means that an element which enters first in a collection will exit last.

As with a Queue collection, we can use the System.Collection and System.Collection.Generic namespaces:

Stack stack = new Stack();
Stack<int> stackInt = new Stack<int>();

Related Methods and Properties

The Push method inserts an object at the top of the collection:

Stack stack1 = new Stack();
stack1.Push("Fifty Five");

foreach (var item in stackCollection1)

Pop removes the element which was included last in a collection and returns it:
Stack stackCollection2 = new Stack();
stackCollection2.Push("Fifty Five");

double number = Convert.ToDouble(stackCollection2.Pop());
Console.WriteLine($"Element removed from a collection is: {number}");

foreach (var item in stackCollection2)

Peek returns an object ready to exit the collection, but it doesn’t remove it:
Stack stackCollection3 = new Stack();
stackCollection3.Push("Fifty Five");

double number1 = Convert.ToDouble(stackCollection3.Peek());
Console.WriteLine($"Element returned from a collection is: {number}");

foreach (var item in stackCollection3)

To remove all objects from a collection, we use the Clear method.

If we want to count the number of elements, we use the Count property:



The Hashtable represents a collection of a key-value pair that is organized based on the hash code of the key. Differently, from the queue and stack collections, we can instantiate a hashtable object by using the only System.Collections namespace:

Hashtable hashTable = new Hashtable();

A Hashtable’s constructor has a fifteen overloaded constructors.

Common Methods In The Hashtable Collection

The Add method adds an element with the specified key and value into the collection:

Hashtable hashTable = new Hashtable();
hashTable.Add(Element.First, 174);
hashTable.Add(Element.Second, "Sixty");
hashTable.Add(Element.Third, 124.24);
foreach (var key in hashTable.Keys)
    Console.WriteLine($"Key: {key}, value: {hashTable[key]}");

The Remove method removes the element with the specified key from a collection:
Hashtable hashTable1 = new Hashtable();
hashTable1.Add(Element.First, 174);
hashTable1.Add(Element.Second, "Sixty");
hashTable1.Add(Element.Third, 124.24);


foreach (var key in hashTable1.Keys)
    Console.WriteLine($"Key: {key}, value: {hashTable[key]}");

ContainsKey  determines whether a collection contains a specific key:
if (hashTable.ContainsKey(Element.Second))
      Console.WriteLine($"Collection contains key: {Element.Second} and its value is {hashTable[Element.Second]}");

The ContainsValue method determines whether a collection contains a specific value.

Clear removes all elements from a collection:


Common Properties in the Hashtable Collection

Count property counts the number of elements inside a collection:


Keys property returns all the keys from a collection and the Value property returns all the values from a collection:
Hashtable hashTable2 = new Hashtable();
hashTable2.Add(Element.First, 174);
hashTable2.Add(Element.Second, "Sixty");
hashTable2.Add(Element.Third, 124.24);

var keys = hashTable2.Keys;
foreach (var key in keys)

var values = hashTable2.Values;
foreach (var value in values)


In this article, we have learned:

  • To use the Queue collection with its methods
  • To use the Stack collection with its methods
  • How to use Hashtable collection with its methods

In the next article, we are going to talk about List and Dictionary in C#List and Dictionary in C#.