CSharp.Net-Informations.com

  How to use C# Queue Class




   Categories

    HOME
    VB.NET
    CSHARP


   














How to use C# Queue Class

The Queue works like FIFO system , a first-in, first-out collection of Objects. 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.

Some important functions in the Queue Class are follows :

  Enqueue : Add an Item in Queue
  Dequeue : Remove the oldest item from Queue

Peek : Get the reference of the oldest item

Enqueue : Add an Item in Queue

  Syntax : Queue.Enqueue(Object)
  Object : The item to add in Queue

days.Enqueue("Sunday");

Dequeue : Remove the oldest item from Queue (we don't get the item later)

  Syntax : Object Queue.Dequeue()
  Returns : Remove the oldest item and return.

days.Dequeue();

Peek : Get the reference of the oldest item (it is not removed permanently)

  Syntax : Object Queue.Peek()
  returns : Get the reference of the oldest item in the Queue

days.peek();

The following CSharp Source code shows some of commonly used functions :


         C# Source Code Download           Print Source Code
         How to use C# Queue Class - Download
        
C# Tutorial

using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Queue days = new Queue();
            days.Enqueue("Sunday");
            days.Enqueue("Monday");
            days.Enqueue("Tuesday");
            days.Enqueue("Wednsday");
            days.Enqueue("Thursday");
            days.Enqueue("Friday");
            days.Enqueue("Saturday");

            MessageBox.Show (days.Dequeue().ToString ());
            
            if (days.Contains("Monday"))
            {
                MessageBox.Show("The queue contains Monday");
            }
            else
            {
                MessageBox.Show("Does not match any entries");
            }
        }
    }
}
		

When you execute the above C# source code , you will get Sunday in the messagebox and then it check the Monday is exist in the queue or not.

CSharp Collection Related Contents
*     How to use C# ArrayList Class
*     How to use C# HashTable Class
*     How to use C# Queue Class
*     How to use C# NameValueCollection Class
*     How to use Array in C#
*     How to for each loop in C# Array

CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
Search here for more CSharp Source Code :

  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2010 All Rights Reserved