The Queue<T> class in C# implements a first-in, first-out (FIFO) colletcion. Unlike lists, it does not support indexed access or methods like Add() and Remove(), as it doesn't implement IList or ICollection. Instead, it provides specialized operations for queue behavior.
Key members of Queue<T> include:
Enqueue(T item): Adds an element to the end of the queue.Dequeue(): Removes and returns the element at the front of the queue. Throws anInvalidOperationExceptionif the queue is empty.Peek(): Returns the front element without removing it.Count: Gets the number of elements currently in the queue.TrimExcess(): Adjusts the internal capacity to match the actual number of eleemnts.Contains(T item): Checks whether a specific element exists in the queue.CopyTo(T[] array, int index): Copies queue elements into an existing array starting at a specified index.ToArray(): Returns a new array containing all elements in the queue.
To demonstrate practical usage, consider a scenario where people are queued for service. First, define a data model:
[Serializable]
public class Customer : IEquatable<Customer>
{
public string FullName { get; set; }
public string ContactNumber { get; set; }
public Customer() { }
public Customer(string name, string phone)
{
FullName = name;
ContactNumber = phone;
}
public bool Equals(Customer other)
{
if (other == null) return false;
return FullName == other.FullName && ContactNumber == other.ContactNumber;
}
public override bool Equals(object obj) => Equals(obj as Customer);
public override int GetHashCode() => HashCode.Combine(FullName, ContactNumber);
}
Next, encapsulate queue logic in a manager class:
public class ServiceQueue
{
private readonly Queue<Customer> _waitingLine = new Queue<Customer>();
public void EnqueueCustomer(Customer customer)
{
_waitingLine.Enqueue(customer);
}
public Customer ServeNextCustomer()
{
if (_waitingLine.Count == 0)
throw new InvalidOperationException("No customers waiting.");
return _waitingLine.Dequeue();
}
public bool IsCustomerInQueue(Customer customer)
{
return _waitingLine.Contains(customer);
}
public bool HasCustomers() => _waitingLine.Count > 0;
public int GetWaitingCount() => _waitingLine.Count;
}
In a Windows Forms application, this can be integrated as follows:
public partial class MainForm : Form
{
private readonly ServiceQueue _serviceDesk = new ServiceQueue();
private void btnAdd_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string phone = txtPhone.Text.Trim();
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(phone))
{
MessageBox.Show("Please enter both name and phone number.");
return;
}
var newCustomer = new Customer(name, phone);
if (_serviceDesk.IsCustomerInQueue(newCustomer))
{
MessageBox.Show("This customer is already in the queue.");
return;
}
_serviceDesk.EnqueueCustomer(newCustomer);
txtName.Clear();
txtPhone.Clear();
UpdateStatusLabel();
}
private void btnServe_Click(object sender, EventArgs e)
{
if (!_serviceDesk.HasCustomers())
{
MessageBox.Show("No customers in the queue.");
return;
}
var served = _serviceDesk.ServeNextCustomer();
var item = new ListViewItem(served.FullName);
item.SubItems.Add(served.ContactNumber);
listView.Items.Add(item);
UpdateStatusLabel();
}
private void UpdateStatusLabel()
{
tsStatusLabel.Text = $"Waiting: {_serviceDesk.GetWaitingCount()}";
}
}
This implementation ensures that each customer is processed in the order they arrivedd, with real-time updates to the queue count displayed in the status bar.