Thursday 22 March 2012

Queue in c#


//demonstration of Queue Class
using System;
using System.Collections;

class Customer
{
private int no;
private string name,request;

public Customer(int id)
{
no=id;
}

public int Id
{
get
{
return no;
}
}

public string Name
{
get
{
return name;
}
set
{
name=value;
}
}

public string Requestinfo
{
get
{
return request;
}
set
{
request=value;
}
}

}
class Program
{
static void Main()
{
Queue customerDetailsQueue=new Queue();

Customer c;

c=new Customer(1001);
c.Name="Ankur Bhatnagar";
c.Requestinfo="Network problem";
customerDetailsQueue.Enqueue(c);

c=new Customer(1002);
c.Name="Ankit Agarwal";
c.Requestinfo="Desktop problem";
customerDetailsQueue.Enqueue(c);

c=new Customer(1003);
c.Name="Tushar Agarwal";
c.Requestinfo="Mouse problem";
customerDetailsQueue.Enqueue(c);

//To print Queue Items
foreach (object obj in customerDetailsQueue)
        {
Console.WriteLine("Id: {0}, Name: {1},Request: {2}", ((Customer)obj).Id, ((Customer)obj).Name, ((Customer)obj).Requestinfo);
        }

Console.WriteLine();

for(int i=0;i<customerDetailsQueue.Count;)
{
Customer obj= (Customer)customerDetailsQueue.Dequeue();
Console.WriteLine("Id: {0} Name: {1} Problem:{2}",obj.Id,obj.Name,obj.Requestinfo);
}
}
}

No comments:

Post a Comment