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
- 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,
2. 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.
- Queue < int > StudentRollNos = new Queue < int > ();
-
- StudentRollNos.Enqueue(101);
- StudentRollNos.Enqueue(102);
- StudentRollNos.Enqueue(103);
- StudentRollNos.Enqueue(104);
- StudentRollNos.Enqueue(105);
- Console.WriteLine("Studnet Roll List");
- foreach(int studentRoll in StudentRollNos)
- {
- Console.WriteLine("Roll No. : " + studentRoll.ToString());
- }
- Console.WriteLine("End of Studnet Roll List");
- Console.ReadKey();
b. DeQueue: To remove first object from the Queue List.
- StudentRollNos.Dequeue();
c. Peek: To get the first object from Queue List.
d. ToArray: To convert your QUEUE list to an array.
- Var StudentArray = StudentRollNos.ToArray();
e. TrimExcess: To reduce the uses of QUEUE memory usage.
f. Count: To get the number of objects in QUEUE.
g. Clear: To erase / remove all the objects from the QUEUE.
h. Contains: To check whether an object is in the Queue.
Code - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace QueuesConsoleApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- Queue < int > StudentRollNos = new Queue < int > ();
-
- StudentRollNos.Enqueue(101);
- StudentRollNos.Enqueue(102);
- StudentRollNos.Enqueue(103);
- StudentRollNos.Enqueue(104);
- StudentRollNos.Enqueue(105);
- Console.WriteLine("Studnet Roll List");
- foreach(int studentRoll in StudentRollNos)
- {
- Console.WriteLine("Roll No. : " + studentRoll.ToString());
- }
- Console.WriteLine("End of Studnet Roll List");
- Console.ReadKey();
- Console.WriteLine("Removing Studnet From List with Command DEQUEUE");
- StudentRollNos.Dequeue();
- Console.WriteLine("Print Again After Removing Studnet From List");
- foreach(int studentRoll in StudentRollNos)
- {
- Console.WriteLine("Roll No. : " + studentRoll.ToString());
- }
- Console.WriteLine("You Can See : 101 has been Removed Studnet From List");
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("Display First Student Roll Number Studnet From List with Command PEEK");
- int PeekNo = StudentRollNos.Peek();
- Console.WriteLine("Peek Command Roll No. " + PeekNo.ToString());
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("Now, Convertig QUEUES into ARRARY with Command ToArray");
- var StudentArray = StudentRollNos.ToArray();
- Console.WriteLine("Printing Array named STUDENTARRAY ");
- for (int i = 0; i != StudentArray.Length; i++)
- {
- Console.WriteLine("Roll No. : " + StudentArray[i].ToString());
- }
- Console.ReadKey();
- Console.WriteLine("Printed Each Array named STUDENTARRAY ");
- Console.WriteLine();
- Console.WriteLine();
- Console.ReadKey();
-
- Console.WriteLine("Total Number of Student " + StudentRollNos.Count().ToString());
- bool FoundValue101 = StudentRollNos.Contains(101);
- if (FoundValue101 == true)
- {
- Console.WriteLine("Roll No.101 Found");
- }
- else
- {
- Console.WriteLine("Roll No.101 Not Found");
- }
- Console.ReadKey();
- bool FoundValue102 = StudentRollNos.Contains(102);
- if (FoundValue102 == true)
- {
- Console.WriteLine("Roll No.102 Found");
- }
- else
- {
- Console.WriteLine("Roll No.102 Not Found");
- }
- Console.ReadKey();
- }
- }
- }
Console Output