0% found this document useful (0 votes)
4 views2 pages

C# FIFO Queue Usage Guide

The Queue class in C# implements a first-in, first-out (FIFO) collection. Objects are added to one end of the queue and removed from the other. The class provides methods to enqueue objects, dequeue objects, and peek at the first object without removing it. Some key methods are Enqueue to add an object, Dequeue to remove the oldest object, and Peek to view the oldest object without removing it. The document provides an example using a queue to store days of the week, demonstrating Enqueue, Dequeue, and checking for a contained object.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

C# FIFO Queue Usage Guide

The Queue class in C# implements a first-in, first-out (FIFO) collection. Objects are added to one end of the queue and removed from the other. The class provides methods to enqueue objects, dequeue objects, and peek at the first object without removing it. Some key methods are Enqueue to add an object, Dequeue to remove the oldest object, and Peek to view the oldest object without removing it. The document provides an example using a queue to store days of the week, demonstrating Enqueue, Dequeue, and checking for a contained object.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

How to use C# Queue Class

The Queue works like FIFO system , a first-in, firstout collection of Objects. Objects stored in a Queue are inserted
at one end and removed from the other. The Queue provide
additional insertion, extraction, and inspection operations. We
can Enqueue (add) items in Queue and we
can Dequeue (remove from Queue ) or we can Peek (that is we
will get the reference of first item ) item from Queue. Queue
accepts null reference as a valid value and allows duplicate
elements.
Some important functions in the Queue Class are follows :
Enqueue : Add an Item in Queue
Dequeue : Remove the oldest item from Queue
Peek

: Get the reference of the oldest item

Enqueue : Add an Item in Queue


Syntax : [Link](Object)
Object : The item to add in Queue
[Link]("Sunday");

Dequeue : Remove the oldest item from Queue (we don't


get the item later)
Syntax : Object [Link]()
Returns : Remove the oldest item and return.
[Link]();

Peek : Get the reference of the oldest item (it is not


removed permanently)
Syntax : Object [Link]()
returns : Get the reference of the oldest item in the Queue
[Link]();

The following CSharp Source code shows some of commonly used


functions :
using System;
using [Link];

using [Link];
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Queue days = new Queue();
[Link]("Sunday");
[Link]("Monday");
[Link]("Tuesday");
[Link]("Wednsday");
[Link]("Thursday");
[Link]("Friday");
[Link]("Saturday");
[Link] ([Link]().ToString ());
if ([Link]("Monday"))
{
[Link]("The queue contains Monday");
}
else
{
[Link]("Does not match any entries");
}
}
}

You might also like