Queue and Stack Collection Classes in C#

Generic Queue Collection Class: A Queue is a First in First Out (FIFO) collection class in the System.Collections.Generic namespace.

Let's have a look at the following example.

Create a new console application and add a class “Employee” with two auto-implemented properties.
  1. class Employee  
  2.    {  
  3.       public int Id { getset; }  
  4.       public string Name { getset; }  
  5.     } 
In the same class file, create another class “MainProgram” and create a Main method.
  1. class Program  
  2.    {  
  3.       static void Main(string[] args)  
  4.         {  
  5.   
  6.         }  

In this Main method create three objects of the Employee class.
  1. static void Main(string[] args)  
  2.      {  
  3.        Employee employeeOne = new Employee()  
  4.      {  
  5.         Id = 1,  
  6.        Name = "Aiden"  
  7.      };  
  8.   
  9. Employee employeeTwo = new Employee()   
  10.     {  
  11.        Id = 2,   
  12.        Name = "Jack"  
  13.      };  
  14.   
  15. Employee employeeThree = new Employee()  
  16.     {  
  17.       Id = 3,  
  18.       Name = "Tom Clancy"  
  19.     };  
  20.   

To create a List of Employees, we say:
  1. List<Employee> employeeList = new List<Employee>(); 
Where Employee is the type.

Just like that, to create an Employee queue collection object, we say:
  1. Queue<Employee> employeeQueue = new Queue<Employee>(); 
Enqueue Method: To add items to a list we use the Add method and to add items to a queue, we use the Enqueue method.
 
 
This Enqueue method adds objects to the end of the collection queue one after another. Look at the type of item this enqueue parameter expects, an item/object of type Employee.
  1. Queue<Employee> employeeQueue = new Queue<Employee>();  
  2. employeeQueue.Enqueue(employeeOne);  
  3. employeeQueue.Enqueue(employeeTwo);  
  4. employeeQueue.Enqueue(employeeThree); 
Dequeue Method: To add an item, we use the Enqueue method and to return an item we use Dequeue method.
 
  
 
But this dequeue method does not just return an item, it removes the item from the queue and returns it back at the beginning.
  1. int itemsBeforeDequeue = employeeQueue.Count;  
  2. Console.WriteLine("Items before Dequeue {0}",itemsBeforeDequeue);  
  3.   
  4. Employee empOne = employeeQueue.Dequeue();  
  5. Console.WriteLine(empOne.Id+" "+empOne.Name);  
  6.   
  7. int itemsAfterDequeue = employeeQueue.Count;  
  8. Console.WriteLine("Items after Dequeue {0}",itemsAfterDequeue); 
Run the application.
 
 
 
In the output, before we invoke the Dequeue method, the total items was 3 but after the Dequeue method, the total items is 2. Just like that if we again invoke the Dequeue method it will return the second employee object back because now the second employee object is in the first position of the queue.
  1. Employee empTwo = employeeQueue.Dequeue();  
  2. Console.WriteLine(empTwo.Id + " " + empTwo.Name);  
  3.   
  4. int itemsAfterFirstDequeue = employeeQueue.Count;  
  5. Console.WriteLine("Items after second Dequeue {0}", itemsAfterFirstDequeue); 
Run the application.
 
 

So, now there is just one item left in the queue collection.

Foreach loop: To retrieve all the items from a list collection, we use a foreach loop and just like that to retrieve all the items from a queue collection, we can use a foreach loop.

NOTE

This foreach loop just loops thru each item in a collection, but it will not remove or dequeue any of the items/objects.
  1. foreach(Employee emp in employeeQueue)  
  2.    {  
  3.      Console.WriteLine(emp.Id + " "+emp.Name);  
  4.    } 
 
 
Peek: To remove and return an item at the beginning we use the Dequeue method but if we want to retrieve or return an item at the beginning without removing it, we can use the Peek method.
  1. int itemBeforePeek = employeeQueue.Count();  
  2. Console.WriteLine("Items before Peek {0}", employeeQueue.Count);  
  3.   
  4. Employee employeePeek = employeeQueue.Peek();  
  5. Console.WriteLine(employeePeek.Name);  
  6.   
  7. int itemAfterPeek = employeeQueue.Count();  
  8. Console.WriteLine("Items After Peek {0}", employeeQueue.Count); 
Run the application.
 
 
 
The following is a code snippet for the Main method.
  1.  static void Main(string[] args)      
  2.         {    
  3.           Employee employeeOne = new Employee() {    
  4.           Id = 1,    
  5.          Name = "Aiden"    
  6.         };    
  7.         
  8.  Employee employeeTwo = new Employee()     
  9.          {    
  10.              Id = 2,     
  11.             Name = "Jack"    
  12.          };    
  13.         
  14.  Employee employeeThree = new Employee()     
  15.        {    
  16.            Id = 3,    
  17.           Name = "Tom Clancy"    
  18.        };    
  19.         
  20.  Queue<Employee> employeeQueue = new Queue<Employee>();    
  21.      employeeQueue.Enqueue(employeeOne);    
  22.      employeeQueue.Enqueue(employeeTwo);    
  23.      employeeQueue.Enqueue(employeeThree);    
  24.       
  25. #region Dequeue method    
  26.     int itemsBeforeDequeue = employeeQueue.Count;    
  27.     Console.WriteLine("Items before Dequeue {0}", itemsBeforeDequeue);    
  28.         
  29.     Employee empOne = employeeQueue.Dequeue();    
  30.     Console.WriteLine(empOne.Id + " " + empOne.Name);    
  31.         
  32.     int itemsAfterDequeue = employeeQueue.Count;    
  33.     Console.WriteLine("Items after first Dequeue {0}", itemsAfterDequeue);    
  34.         
  35.     Employee empTwo = employeeQueue.Dequeue();    
  36.     Console.WriteLine(empTwo.Id + " " + empTwo.Name);    
  37.         
  38.     int itemsAfterFirstDequeue = employeeQueue.Count;    
  39.     Console.WriteLine("Items after second Dequeue {0}", itemsAfterFirstDequeue);    
  40.      
  41. #endregion    
  42.       
  43. #region foreach loop    
  44.     foreach(Employee emp in employeeQueue)  
  45.    {    
  46.     Console.WriteLine(emp.Id + " " + emp.Name);    
  47.     }    
  48.   
  49. #endregion    
  50.       
  51. #region peek method    
  52.     int itemBeforePeek = employeeQueue.Count();    
  53.     Console.WriteLine("Items before Peek {0}", employeeQueue.Count);    
  54.         
  55.     Employee employeePeek = employeeQueue.Peek();    
  56.     Console.WriteLine(employeePeek.Name);    
  57.         
  58.     int itemAfterPeek = employeeQueue.Count();    
  59.     Console.WriteLine("Items After Peek {0}", employeeQueue.Count);    
  60. #endregion    
  61.     }  
Generic Stack Collection Class: A stack is a Last In First Out (LIFO) collection class present in the System.Collection.Generic namespace.

Let's have a look at the following example.

Create a new console application and add a class “Employee” with two auto-implemented properties.
  1. class Employee  
  2.    {    
  3.      public int Id { getset; }    
  4.      public string Name { getset; }    
  5.    }   
  6.     
  7. //In the same class file, create another class “MainProgram” and create a Main method .    
  8.   
  9. class Program {    
  10. static void Main(string[] args)   
  11.    {    
  12.     
  13.    }    
  14. }    
  15.   
  16. //In this Main method create three different objects of Employee class.   
  17.    
  18. static void Main(string[] args)  
  19.   {     
  20.     Employee employeeOne = new Employee() {    
  21.     Id = 1,     
  22.     Name = "Aiden"    
  23.   };    
  24.     
  25.    Employee employeeTwo = new Employee()  
  26.  {    
  27.     Id = 2,    
  28.    Name = "Jack"    
  29.  };    
  30.     
  31.    Employee employeeThree = new Employee()  
  32.  {    
  33.    Id = 3,    
  34.   Name = "Tom Clancy"    
  35.  };    
  36.     
  37. }   
To create an Employee stack collection object, we say:
  1. Stack<Employee> EmployeeStack = new Stack<Employee>(); 
Push Method: To add items to a stack, we use the Push method.
 

This Push method adds objects to the top of the collection, in other words one over another. Look at the type of item this Push parameter expects, an item/object of type Employee.
  1. Stack<Employee> EmployeeStack = new Stack<Employee>();  
  2. EmployeeStack.Push(employeeOne);  
  3. EmployeeStack.Push(employeeTwo);  
  4. EmployeeStack.Push(employeeThree); 
Pop Method: To add an item, we use the Push method and to return an item we use the Pop method.
 
 
 
But this Pop method does not just return an item. It removes the item from the stack collection and returns it back at the top.
  1. int itemsBeforeStack = EmployeeStack.Count;  
  2. Console.WriteLine("Items before Dequeue {0}", itemsBeforeStack);  
  3.   
  4. Employee empOne = EmployeeStack.Pop();  
  5. Console.WriteLine(empOne.Id + " " + empOne.Name);  
  6.   
  7. int itemsAfterStack = EmployeeStack.Count;  
  8. Console.WriteLine("Items after Dequeue {0}", itemsAfterStack); 
Run the application.
 
 
 
In the output, before we invoked the Pop method, the total item was 3 but after the Pop method, the total item is 2 and the last item is removed and returned first, in other words Last In First Out.

Foreach loop: To retrieve all the items from a list collection, we use a foreach loop and just like that to retrieve all the items from a stack collection, we can use a foreach loop.

NOTE

This foreach loop just loops thru each item in a collection, but it will not remove any of the items/objects.
  1. foreach(Employee employee in EmployeeStack) {  
  2. Console.WriteLine(employee.Id + " "+employee.Name);  

 
 
Peek : To remove and return an item at the beginning we use the Pop method but if we want to retrieve or return an item at the beginning without removing it, we can use the Peek method.
  1. int itemBeforePeek = EmployeeStack.Count();  
  2. Console.WriteLine("Items before Peek {0}", EmployeeStack.Count);  
  3.   
  4. Employee employeePeek = EmployeeStack.Peek();  
  5. Console.WriteLine(employeePeek.Name);  
  6.   
  7. int itemAfterPeek = EmployeeStack.Count();  
  8. Console.WriteLine("Items After Peek {0}", EmployeeStack.Count); 
Run the application.
 
 

Code snippet for the Main method.
  1. static void Main(string[] args) {  
  2. Employee employeeOne = new Employee() {  
  3. Id = 1,  
  4. Name = "Aiden"  
  5. };  
  6.   
  7. Employee employeeTwo = new Employee() {  
  8. Id = 2,  
  9. Name = "Jack"  
  10. };  
  11.   
  12. Employee employeeThree = new Employee() {  
  13. Id = 3,  
  14. Name = "Tom Clancy"  
  15. };  
  16.   
  17. Stack<Employee> EmployeeStack = new Stack<Employee>();  
  18. EmployeeStack.Push(employeeOne);  
  19. EmployeeStack.Push(employeeTwo);  
  20. EmployeeStack.Push(employeeThree);  
  21.  
  22. #region Pop Method  
  23. int itemsBeforeStack = EmployeeStack.Count;  
  24. Console.WriteLine("Items before Dequeue {0}", itemsBeforeStack);  
  25.   
  26. Employee empOne = EmployeeStack.Pop();  
  27. Console.WriteLine(empOne.Id + " " + empOne.Name);  
  28.   
  29. int itemsAfterStack = EmployeeStack.Count;  
  30. Console.WriteLine("Items after Dequeue {0}", itemsAfterStack);  
  31. #endregion  
  32.  
  33.  
  34. #region Foreach loop  
  35. foreach(Employee employee in EmployeeStack) {  
  36. Console.WriteLine(employee.Id + " " + employee.Name);  
  37. }  
  38. #endregion  
  39.  
  40.  
  41. #region Peek Method  
  42. int itemBeforePeek = EmployeeStack.Count();  
  43. Console.WriteLine("Items before Peek {0}", EmployeeStack.Count);  
  44.   
  45. Employee employeePeek = EmployeeStack.Peek();  
  46. Console.WriteLine(employeePeek.Name);  
  47.   
  48. int itemAfterPeek = EmployeeStack.Count();  
  49. Console.WriteLine("Items After Peek {0}", EmployeeStack.Count);  
  50.  
  51. #endregion  
  52.   

Summary

In this article we learned how to work with the Queue and Stack collection classes.

I hope you like it.
Thank you.