Use Queue in C# Language

Introduction

Queue  class represent  a first-in, first-out in the collection. You can add an item in the list ,it is called to enqueue, and when you  remove an item, called the deque.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace what_is_queue_in_c_sharp

{

      

        class queue_class    //create class

        {

            static void Main(string[] args)

            {

                Queue qu_obj = new Queue(); // create instance of queue

 

                qu_obj.Enqueue("Nitin");        //add value in queue

                qu_obj.Enqueue("Arvind");

                qu_obj.Enqueue("Vikas");

                qu_obj.Enqueue("Ravi");

 

                Console.WriteLine("        Current queue:       \n ");

                foreach (string c in qu_obj)

                {

                    Console.Write(c + ",  ");   // display value of queue

                }

                Console.WriteLine();

               

                qu_obj.Enqueue("Sunny");  // add another value in queue

                Console.WriteLine("\nThe Last Enqueue value in queue:\n {0}", "sunny");

                Console.WriteLine("\nAfter push queue:\n ");

                foreach (string c in qu_obj)

                {

                    Console.Write(c + ",  ");    // show value after add another value

                }

 

                qu_obj.Dequeue();   // Removing two value in queue ;

                qu_obj.Dequeue();

 

 

                Console.WriteLine("\n\nAfter removing Two values Queue:\n ");

                foreach (string c in qu_obj)

                {

                    Console.Write(c + ",  "); // show value after remove

                }

                Console.WriteLine("\n");

                Console.WriteLine();

            }

        }

 }

Output


output.jpg

Ebook Download
View all
Learn
View all