Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

C# Arrays

C# Character

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

C# ArrayList

C# Stack

C# Queue

C# HashTable

C# SortedList

Page Stats

Visitor: 133

Queue Class

The System.Collections.Queue class follow the concept of First In First Out (FIFO), means that the item inserted first is processed first. To add an element is termed as Enqueue, and removal of an element from the queue is termed as 'Dequeue'. Concept of queue is used in printing when multiple users share same printer, if multiple request are send printer follow the concept of FIFO.

List some important properties of Queue
S.No.PropertyDescription
countReturns the total count of elements in the Queue.
List some important methods of Stack
S.No.MethodSyntaxDescription
Enqueue()void Enqueue(object obj)Adds an item into the queue.
Dequeue()object Dequeue()Removes and returns an item from the beginning of the queue.
Peek()object Peek();Returns an first item from the queue
Contains()bool Contains(object obj);Checks whether an item exists in the queue or not.
Clear()void Clear();Removes all items from the queue.
Enqueue() - Add element in Queue

To add an element in Queue use Enqueue() method, you can add elements of any datatype.

Example 1: Add elements in the Queue.

Queue qu = new Queue();
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Enqueue("forty");

foreach(var item in qu)
{
	Console.WriteLine(item);
}
10
20
30
forty
Dequeue() - Removes element in Queue

Dequeue() removes and returns a first element from a queue. Calling Dequeue() method on empty queue will throw InvalidOperation exception.

Example 2: Delete element from the Queue.

Queue qu = new Queue();
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Enqueue("forty");

Console.WriteLine("Number of elements in the Queue: {0}", qu.Count);

while (qu.Count > 0)
    Console.WriteLine(qu.Dequeue());

Console.WriteLine("Number of elements in the Queue: {0}", qu.Count);
Number of elements in the Queue: 4
10
20
30
forty
Number of elements in the Queue: 0
Peek() Method - Queue

The Peek() method always returns the first element from a queue without removing it. Calling Peek() methods on an empty queue will throw a run time exception "InvalidOperationException".

Example 3: Display first element from the Queue.

Queue qu = new Queue();
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Enqueue("forty");

Console.WriteLine("Number of elements in the Queue: {0}", qu.Count);
Console.WriteLine(qu.Peek());
Console.WriteLine(qu.Peek());
Console.WriteLine("Number of elements in the Queue: {0}", qu.Count);
Number of elements in the Queue: 4
10
10
Number of elements in the Queue: 4
Contains() Method - Queue

The Contains() method checks whether an item exists in a queue or not. It returns true if the specified item exists, otherwise it returns false.

Example 4: Check element exits in a Queue or not.

Queue qu = new Queue();
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Enqueue("forty");

queue.Contains(22); // true
queue.Contains(100); //false
Clear() Method - Queue

The Clear() method removes all the items from a queue.

Example 5: Removes all items from a queue.

Queue qu = new Queue();
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Enqueue("forty");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);
queue.Clear();
Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);
Number of elements in the Queue: 4
Number of elements in the Queue: 0
Advertisement
Updated: 06-Sep-21