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:

And by using System.Collection namespace:

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:

When we want to  remove an element at the beginning of the collection and return it, we are going to use the Dequeue method:

The Peek method returns the element at the beginning of the collection but does not remove it:

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:

Related Methods and Properties

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

Pop removes the element which was included last in a collection and returns it:

Peek returns an object ready to exit the collection, but it doesn’t remove it:

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:

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:

The Remove method removes the element with the specified key from a collection:

ContainsKey  determines whether a collection contains a specific key:

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:


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#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.