Csharp Queue
[ C# Collections](#)
In C#, a Queue is a First-In-First-Out (FIFO) data structure.
The Queue belongs to the `System.Collections` or `System.Collections.Generic` namespaces, providing non-generic and generic implementations respectively. Queues are suitable for scenarios where data needs to be processed in the order it was added.
A Queue represents a collection of objects that are stored and accessed in a **First-In-First-Out** manner. Use a Queue when you need to access items in a FIFO order. When you add an item to the list, it is called **enqueueing**, and when you remove an item from the list, it is called **dequeueing**.
### Features
* **First-In-First-Out**: The earliest element added to the queue is the first one to be removed.
* **Dynamic Size**: The capacity of the queue adjusts dynamically as needed.
* **Generic Support**: Using `Queue` allows you to store strongly-typed elements.
* **Thread Safety**: The `Queue` class itself is not thread-safe, but you can use `ConcurrentQueue` for thread-safe operations.
The following table lists some common **properties** of the **Queue** class:
| Property Name | Type | Description |
| --- | --- | --- |
| `Count` | `int` | Gets the number of elements in the queue. |
| `SyncRoot` | `object` | Gets an object that can be used to synchronize access to the queue (non-generic). |
| `IsSynchronized` | `bool` | Indicates whether access to the queue is synchronized (thread-safe, always `false`). |
The following table lists some common **methods** of the **Queue** class:
| Method Name | Return Type | Description |
| --- | --- | --- |
| **Element Operations** | | |
| `Enqueue(object item)` | `void` | Adds an object to the end of the queue. |
| `Dequeue()` | `object` | Removes and returns the object at the beginning of the queue. |
| `Peek()` | `object` | Returns the object at the beginning of the queue without removing it. |
| `Clear()` | `void` | Removes all objects from the queue. |
| **Checking and Copying** | | |
| `Contains(object item)` | `bool` | Determines whether an element is in the queue. |
| `ToArray()` | `object[]` | Copies the queue elements to a new array. |
| `Clone()` | `object` | Creates a shallow copy of the queue. |
| `CopyTo(Array array, int index)` | `void` | Copies the queue elements to an existing array, starting at a specified index. |
| **Enumerator Support** | | |
| `GetEnumerator()` | `IEnumerator` | Returns an enumerator that iterates through the queue. |
| **Thread Safety** | | |
| `Synchronized(Queue queue)` | `Queue` | Returns a thread-safe wrapper for the queue. |
The following examples demonstrate the usage of a Queue:
## Example 1
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue =new Queue();
// Add elements
queue.Enqueue("First");
queue.Enqueue("Second");
queue.Enqueue("Third");
// View the front element
Console.WriteLine($"Peek: {queue.Peek()}");// Output: First
// Remove elements
Console.WriteLine($"Dequeue: {queue.Dequeue()}");// Output: First
// Remaining elements
foreach(var item in queue)
{
Console.WriteLine(item);// Output: Second, Third
}
// Check for containment
Console.WriteLine($"Contains 'Second': {queue.Contains("Second")}");// Output: True
// Convert to array
object[] array = queue.ToArray();
Console.WriteLine($"Array Length: {array.Length}");// Output: 2
}
}
## Example 2
using System;
using System.Collections;
namespace CollectionsApplication
{
class Program
{
static void Main(string[] args)
{
Queue q =new Queue();
q.Enqueue('A');
q.Enqueue('M');
q.Enqueue('G');
q.Enqueue('W');
Console.WriteLine("Current queue: ");
foreach(char c in q)
Console.Write(c +" ");
Console.WriteLine();
q.Enqueue('V');
q.Enqueue('H');
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 M G W Current queue: A M G W V H Removing values The removed value: A The removed value: M
[ C# Collections](#)
YouTip