How to implement Queue in C#

By Sourav Kayal Jun 29, 2013
In this article i am going to show how to implement queue collection 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.

Sourav Kayal

I am .NET learner passionate logger, like to .NET eat and sleep.  Have little experience in Web technology with new development platform including node.js, MVC Web API. Feel free to teach me .NET.

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs