In Focus

QUEUE In C#

In this article you will learn about QUEUE in C#. Queue is FIFO (First In First Out) collection type & Generic class. Queue collection classes automatically size as required.

 

In this article you will learn about QUEUE.

Queue is FIFO (First In First Out) collection type & Generic class. Queue collection classes automatically size as required.

Syntax

Queue<T>

Example
  1. Queue<int> rollno = new Queue<int>();
I like QUEUE collection, FIFO is also one kind of method to maintain the stock. There is no extra code required to follow FIFO. It is a readymade tool. You will see uses of QUEUE step by step.

I had created CONSOLE APPLICATION to work with QUEUE.

1. File, New, then Console Application.

Named QueuesConsoleApplicaton,

Console Application

2. Queues,

Queues

3. The following are Methods and Properties of QUEUE,
  • Enqueue
  • DeQueue
  • Peek
  • ToArray
  • TrimExcess
  • Count
  • Clear
  • Contains

a. Enqueue : To add a new object at the end of the Queue List.

Enqueue

  1. Queue < int > StudentRollNos = new Queue < int > ();  
  2. //Enqueue to Add New Item in Queue List  
  3. StudentRollNos.Enqueue(101);  
  4. StudentRollNos.Enqueue(102);  
  5. StudentRollNos.Enqueue(103);  
  6. StudentRollNos.Enqueue(104);  
  7. StudentRollNos.Enqueue(105);  
  8. Console.WriteLine("Studnet Roll List");  
  9. foreach(int studentRoll in StudentRollNos)  
  10. {  
  11.     Console.WriteLine("Roll No. : " + studentRoll.ToString());  
  12. }  
  13. Console.WriteLine("End of Studnet Roll List");  
  14. Console.ReadKey();  
b. DeQueue: To remove first object from the Queue List.

DeQueue
  1. StudentRollNos.Dequeue();  
c. Peek: To get the first object from Queue List.

Peek
  1. StudentRollNos.Peek();  
d. ToArray: To convert your QUEUE list to an array.

ToArray
  1. Var StudentArray = StudentRollNos.ToArray();  
e. TrimExcess: To reduce the uses of QUEUE memory usage.

TrimExcess

f. Count: To get the number of objects in QUEUE.

Count
  1. StudentRollNos.Count()  
g. Clear: To erase / remove all the objects from the QUEUE.

Clear
  1. StudentRollNos.Clear()  
h. Contains: To check whether an object is in the Queue.

Contains

Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace QueuesConsoleApplication  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             Queue < int > StudentRollNos = new Queue < int > ();  
  12.             //Enqueue to Add New Item in Queue List  
  13.             StudentRollNos.Enqueue(101);  
  14.             StudentRollNos.Enqueue(102);  
  15.             StudentRollNos.Enqueue(103);  
  16.             StudentRollNos.Enqueue(104);  
  17.             StudentRollNos.Enqueue(105);  
  18.             Console.WriteLine("Studnet Roll List");  
  19.             foreach(int studentRoll in StudentRollNos)  
  20.             {  
  21.                 Console.WriteLine("Roll No. : " + studentRoll.ToString());  
  22.             }  
  23.             Console.WriteLine("End of Studnet Roll List");  
  24.             Console.ReadKey();  
  25.             Console.WriteLine("Removing Studnet From List with Command DEQUEUE");  
  26.             StudentRollNos.Dequeue();  
  27.             Console.WriteLine("Print Again After Removing Studnet From List");  
  28.             foreach(int studentRoll in StudentRollNos)  
  29.             {  
  30.                 Console.WriteLine("Roll No. : " + studentRoll.ToString());  
  31.             }  
  32.             Console.WriteLine("You Can See : 101 has been Removed Studnet From List");  
  33.             Console.ReadKey();  
  34.             Console.WriteLine();  
  35.             Console.WriteLine();  
  36.             Console.WriteLine("Display First Student Roll Number Studnet From List with Command PEEK");  
  37.             int PeekNo = StudentRollNos.Peek();  
  38.             Console.WriteLine("Peek Command Roll No. " + PeekNo.ToString());  
  39.             Console.ReadKey();  
  40.             Console.WriteLine();  
  41.             Console.WriteLine();  
  42.             Console.WriteLine("Now, Convertig QUEUES into ARRARY with Command ToArray");  
  43.             var StudentArray = StudentRollNos.ToArray();  
  44.             Console.WriteLine("Printing Array named STUDENTARRAY ");  
  45.             for (int i = 0; i != StudentArray.Length; i++)  
  46.             {  
  47.                 Console.WriteLine("Roll No. : " + StudentArray[i].ToString());  
  48.             }  
  49.             Console.ReadKey();  
  50.             Console.WriteLine("Printed Each Array named STUDENTARRAY ");  
  51.             Console.WriteLine();  
  52.             Console.WriteLine();  
  53.             Console.ReadKey();  
  54.             //StudentRollNos.Count();  
  55.             Console.WriteLine("Total Number of Student " + StudentRollNos.Count().ToString());  
  56.             bool FoundValue101 = StudentRollNos.Contains(101);  
  57.             if (FoundValue101 == true)  
  58.             {  
  59.                 Console.WriteLine("Roll No.101 Found");  
  60.             }  
  61.             else  
  62.             {  
  63.                 Console.WriteLine("Roll No.101 Not Found");  
  64.             }  
  65.             Console.ReadKey();  
  66.             bool FoundValue102 = StudentRollNos.Contains(102);  
  67.             if (FoundValue102 == true)  
  68.             {  
  69.                 Console.WriteLine("Roll No.102 Found");  
  70.             }  
  71.             else  
  72.             {  
  73.                 Console.WriteLine("Roll No.102 Not Found");  
  74.             }  
  75.             Console.ReadKey();  
  76.         }  
  77.     }  
  78. }  
Console Output

Output