top

Search

C# Tutorial

The Queue Collection is used when the data items need to be arranged in a FIFO (First In First Out) manner. When a data item is entered into the collection, it is called enqueue. When the data item is removed from the collection, it is called dequeue.Constructors in Queue CollectionThe different constructors and their description is given as follows:Table: Constructors in Queue Collection in C#ConstructorsDescriptionQueue<T>()This constructor initializes a new instance of the Queue<T> class that is empty and has the default initial capacity.Queue<T>(IEnumerable<T>)This constructor initializes a new instance of the Queue<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.Queue<T>(Int32)This constructor initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.Properties in Queue CollectionThe different properties and their description is given as follows:Table: Properties in Queue Collection in C#PropertiesDescriptionCountThis property gets the number of elements contained in the Queue<T>.Methods in Queue CollectionThe different methods and their description is given as follows:Table: Methods in Queue Collection in C#Source: MSDNMethodsDescriptionClear()This method removes all objects from the Queue<T>.Contains(T)This method determines whether an element is in the Queue<T> or not.CopyTo(T[], Int32)This method copies the Queue<T> elements to an existing one-dimensional Array, starting at the specified array index.Dequeue()This method removes and returns the object at the beginning of the Queue<T>.Enqueue(T)This method adds an object to the end of the Queue<T>.Equals(Object)This method determines whether the specified object is equal to the current object.GetEnumerator()This method returns an enumerator that iterates through the Queue<T>.GetHashCode()This method serves as the default hash function.GetType()This method gets the Type of the current instance.MemberwiseClone()This method creates a shallow copy of the current Object.Peek()This method returns the object at the beginning of the Queue<T> without removing it.ToArray()This method copies the Queue<T> elements to a new array.ToString()This method returns a string that represents the current object.TrimExcess()This method sets the capacity to the actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity.Queue OperationsSome of the stack operations are given as follows:EnqueueAn element is added to the end of the queue using the Enqueue() method. The program that demonstrates this is given as follows:Source Code: Program to enqueue elements in Queue in C#using System; using System.Collections; namespace QueueDemo {   class Example   {      static void Main(string[] args)      {         Queue q = new Queue();         q.Enqueue(1);         q.Enqueue(2);         q.Enqueue(3);         q.Enqueue(4);         Console.WriteLine("Queue elements are:");         foreach (int i in q)         {             Console.Write(i + " ");         }      }   } }The output of the above program is as follows:Queue elements are: 1 2 3 4DequeueThe element at the beginning of the queue can be removed using the Dequeue() method. The program that demonstrates this is given as follows:Source Code: Program to dequeue elements in Queue in C#using System; using System.Collections; namespace QueueDemo {   class Example   {      static void Main(string[] args)      {         Queue q = new Queue();         q.Enqueue(1);         q.Enqueue(2);         q.Enqueue(3);         q.Enqueue(4);         Console.Write("Original queue: ");         foreach (int i in q)         {             Console.Write(i + " ");         }         q.Dequeue();         q.Dequeue();         Console.WriteLine();         Console.Write("Queue after Dequeue() operation: ");         foreach (int i in q)         {             Console.Write(i + " ");         }      }   } }The output of the above program is as follows:Original queue: 1 2 3 4 Queue after Dequeue() operation: 3 4PeekThe element at the beginning of the queue can be viewed without removing it using the Peek() method. The program that demonstrates this is given as follows:Source Code: Program to peek beginning element in Queue in C#using System; using System.Collections; namespace QueueDemo {   class Example   {      static void Main(string[] args)      {         Queue q = new Queue();         q.Enqueue(1);         q.Enqueue(2);         q.Enqueue(3);         q.Enqueue(4);         Console.Write("Queue elements are: ");         foreach (int i in q)         {             Console.Write(i + " ");         }         Console.WriteLine();         Console.Write("Element at the beginning of the queue is: {0}", q.Peek());      }   } }The output of the above program is as follows:Queue elements are: 1 2 3 4 Element at the beginning of the queue is: 1Count the number of elements in a QueueTo count the number of elements in a Queue, use the Count property:Source Code: Program to count the number of elements in a Queue in C#using System; using System.Collections; namespace QueueExample {   class Example {      static void Main(string[] args) {         Queue q = new Queue();                 q.Enqueue('A');         q.Enqueue('B');         q.Enqueue('C');         q.Enqueue('D');         q.Enqueue('E');         q.Enqueue('F');         q.Enqueue('G');         q.Enqueue('H');                 Console.WriteLine("Count = {0} ", q.Count);                 Console.ReadKey();      }   } }The output of the above program is as follows:Count = 8
logo

C# Tutorial

Queue Collection in C#

The Queue Collection is used when the data items need to be arranged in a FIFO (First In First Out) manner. When a data item is entered into the collection, it is called enqueue. When the data item is removed from the collection, it is called dequeue.

Constructors in Queue Collection

The different constructors and their description is given as follows:

Table: Constructors in Queue Collection in C#

ConstructorsDescription
Queue<T>()This constructor initializes a new instance of the Queue<T> class that is empty and has the default initial capacity.
Queue<T>(IEnumerable<T>)This constructor initializes a new instance of the Queue<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
Queue<T>(Int32)This constructor initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.

Properties in Queue Collection

The different properties and their description is given as follows:

Table: Properties in Queue Collection in C#

PropertiesDescription
CountThis property gets the number of elements contained in the Queue<T>.

Methods in Queue Collection

The different methods and their description is given as follows:

Table: Methods in Queue Collection in C#

Source: MSDN

MethodsDescription
Clear()This method removes all objects from the Queue<T>.
Contains(T)This method determines whether an element is in the Queue<T> or not.
CopyTo(T[], Int32)This method copies the Queue<T> elements to an existing one-dimensional Array, starting at the specified array index.
Dequeue()This method removes and returns the object at the beginning of the Queue<T>.
Enqueue(T)This method adds an object to the end of the Queue<T>.
Equals(Object)This method determines whether the specified object is equal to the current object.
GetEnumerator()This method returns an enumerator that iterates through the Queue<T>.
GetHashCode()This method serves as the default hash function.
GetType()This method gets the Type of the current instance.
MemberwiseClone()This method creates a shallow copy of the current Object.
Peek()This method returns the object at the beginning of the Queue<T> without removing it.
ToArray()This method copies the Queue<T> elements to a new array.
ToString()This method returns a string that represents the current object.
TrimExcess()This method sets the capacity to the actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity.

Queue Operations

Some of the stack operations are given as follows:

Enqueue

An element is added to the end of the queue using the Enqueue() method. The program that demonstrates this is given as follows:

Source Code: Program to enqueue elements in Queue in C#

using System;
using System.Collections;
namespace QueueDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        Console.WriteLine("Queue elements are:");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
     }
  }
}

The output of the above program is as follows:

Queue elements are:
1 2 3 4

Dequeue

The element at the beginning of the queue can be removed using the Dequeue() method. The program that demonstrates this is given as follows:

Source Code: Program to dequeue elements in Queue in C#

using System;
using System.Collections;
namespace QueueDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        Console.Write("Original queue: ");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
        q.Dequeue();
        q.Dequeue();
        Console.WriteLine();
        Console.Write("Queue after Dequeue() operation: ");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
     }
  }
}

The output of the above program is as follows:

Original queue: 1 2 3 4
Queue after Dequeue() operation: 3 4

Peek

The element at the beginning of the queue can be viewed without removing it using the Peek() method. The program that demonstrates this is given as follows:

Source Code: Program to peek beginning element in Queue in C#

using System;
using System.Collections;
namespace QueueDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        q.Enqueue(4);
        Console.Write("Queue elements are: ");
        foreach (int i in q)
        {
            Console.Write(i + " ");
        }
        Console.WriteLine();
        Console.Write("Element at the beginning of the queue is: {0}", q.Peek());
     }
  }
}

The output of the above program is as follows:

Queue elements are: 1 2 3 4
Element at the beginning of the queue is: 1

Count the number of elements in a Queue

To count the number of elements in a Queue, use the Count property:

Source Code: Program to count the number of elements in a Queue in C#

using System;
using System.Collections;

namespace QueueExample {
  class Example {
     static void Main(string[] args) {
        Queue q = new Queue();
        
        q.Enqueue('A');
        q.Enqueue('B');
        q.Enqueue('C');
        q.Enqueue('D');
        q.Enqueue('E');
        q.Enqueue('F');
        q.Enqueue('G');
        q.Enqueue('H');
        
        Console.WriteLine("Count = {0} ", q.Count);
        
        Console.ReadKey();
     }
  }
}

The output of the above program is as follows:

Count = 8

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial