.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Queue in C#.net

Posted By:abhays       Posted Date: March 19, 2014    Points: 40    Category: Collections    URL: http://www.dotnetspark.com  

Queue in C#.net
 

Queue is a FIFO collection. It processes elements in a first-in, first-out order. Objects stored in a Queue are inserted at one end and removed from the other. The Queue provide additional insertion, extraction, and inspection operations. We can Enqueue (add) items in Queue and we can Dequeue (remove from Queue ) or we can Peek (that is we will get the reference of first item ) item from Queue. Queue accepts null reference as a valid value and allows duplicate elements.

The following table lists some of the commonly used methods of the Queue class

1

public virtual void Clear();
Removes all objects from the Queue.

2

public virtual void Clone ();
Creates a shallow copy of the Queue.

3

public virtual bool Contains( object obj );
Determines whether an element is in the Queue.

4

public virtual object Dequeue();
Removes and returns the object at the beginning of the Queue

5

public virtual void Enqueue( object obj );
Adds an object to the end of the Queue.

6

public virtual Object Peek()

Returns the object at the beginning of the Queue without removing it.

7

public virtual object[] ToArray();
Copies the Queue elements to a new array.

8

public virtual void TrimToSize();
Sets the capacity to the actual number of elements in the Queue.

For more details Check this link

using System;
using System.Collections;

namespace CollectionsApp
{
   class Program
   {
      static void Main(string[] args)
      {
         Queue q = new Queue();

         q.Enqueue('A');
         q.Enqueue('B');
         q.Enqueue('C');
         q.Enqueue('D');
         
         Console.WriteLine("Current queue: ");
         foreach (char c in q)
            Console.Write(c + " ");
         Console.WriteLine();
         q.Enqueue('E');
         q.Enqueue('E');
         Console.WriteLine("Current queue: ");         
         foreach (char c in q)
            Console.Write(c + " ");
         Console.WriteLine();
         Console.WriteLine("Removing some values ");
         char ch = (char)q.Dequeue();
         Console.WriteLine("The removed value: {0}", ch);
         ch = (char)q.Dequeue();
         Console.WriteLine("The removed value: {0}", ch);
         Console.ReadKey();
      }
   }
}

 

 

When the above code is compiled and executed, it produces the following result: 

Current queue:

A B C D

Current queue:

A B C D E F

Removing values

The removed value: A

The removed value: B



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend