Home » C#.Net

Queue.Dequeue() method with example in C#

C# Queue.Dequeue() method: Here, we are going to learn about the Dequeue() method of Queue class in C#.
Submitted by IncludeHelp, on March 30, 2019

C# Queue.Dequeue() method

Queue.Dequeue() method is used to remove an object/element from the beginning of the Queue and returns that object/element.

Syntax:

    Object Queue.Dequeue();

Parameters: None

Return value: Object – it returns the object/element to be removed.

Example:

    declare and initialize a Queue:
    Queue que = new Queue();   

    insertting elements:
    que.Enqueue(100);
    que.Enqueue(200);
    que.Enqueue(300);
    que.Enqueue(400);
    que.Enqueue(500);
    
    removing elements from Queue:
    que.Dequeue();
    que.Dequeue();
    que.Dequeue();

    Output:
    400 500

C# example to remove an object/element from the Queue using Queue.Dequeue() method

using System;
using System.Text;
using System.Collections;

namespace Test
{
    class Program
    {
        //function to print queue elements
        static void printQueue(Queue q)
        {
            foreach (Object obj in q)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            //declare and initialize a Queue
            Queue que = new Queue();

            //insertting elements
            que.Enqueue(100);
            que.Enqueue(200);
            que.Enqueue(300);
            que.Enqueue(400);
            que.Enqueue(500);

            //printing queue elements
            Console.WriteLine("Queue elements are...");
            printQueue(que);

            //removing elements from Queue
            Console.WriteLine(que.Dequeue() + " is removed...");
            Console.WriteLine(que.Dequeue() + " is removed...");
            Console.WriteLine(que.Dequeue() + " is removed...");

            //printing queue elements after removing elements
            Console.WriteLine("Queue elements are...");
            printQueue(que);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Queue elements are...
100 200 300 400 500
100 is removed...
200 is removed...
300 is removed...
Queue elements are...
400 500

Reference: Queue.Dequeue Method






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.