How to implement Queue in C#

Queue Class

Queue handles first in first out data processing mechanism. queue class in one of Collection class of .NET Class library. You have to include using System.Collections.Generic; namespace in order to use collection class in your program.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 namespace BlogProject

{

    class Program

    {

        static void Main(string[] args)

        {

            Queue<Int32> qu = new Queue<int>();

            qu.Enqueue(100);

            qu.Enqueue(200);

            qu.Dequeue();

             Console.WriteLine(qu.Last());

            Console.ReadLine();

        }

    }

}

 Create object of Queue class like this

           Queue<Int32> qu = new Queue<int>();

You can call qu.Enqueue(); function to insert value into your queue.

You can call qu.Dequeue(); function to delete item from your queue. It will delete the first item of the queue what you have inserted.

 Now in this program I have inserted two value at first and deleted one item. And by nature the first item(100) will get delete.

 And ,then the last tem of my Queue is 200. I can get last item of Queue by calling qu.Last() function.

Ebook Download
View all
Learn
View all