What is a C# queue?

A C# Queue is an abstract data structure. It’s like a pipe because it follows the First-In-First-Out methodology, i.e., data that is sent first will be retrieved first.

svg viewer

Commonly used methods

Method Description
clear() It removes all the elements in the queue.
contains(object) It determines whether or not the object is in the queue.
Dequeue() It removes the element from the start of the queue.
Enqueue() It adds the object at the end of the queue.
toArray() It copies the queue to a new array.
TrimToSize() It sets the size of the queue according to the number of elements in the queue.

Code

using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] arguments) {
Queue qu = new Queue();
qu.Enqueue('E');
qu.Enqueue('D');
qu.Enqueue('U');
qu.Enqueue('C');
qu.Enqueue('A');
qu.Enqueue('T');
qu.Enqueue('I');
qu.Enqueue('V');
qu.Enqueue('E');
Console.WriteLine("Current queue: ");
foreach (char c in qu) Console.Write(c + " ");
Console.WriteLine();
qu.Enqueue('I');
qu.Enqueue('N');
qu.Enqueue('C');
qu.Enqueue('.');
Console.WriteLine("Queue after using Enqueue: ");
foreach (char c in qu) Console.Write(c + " ");
Console.WriteLine();
qu.Dequeue();
Console.WriteLine("Queue after using Dequeue: ");
foreach (char c in qu) Console.Write(c + " ");
Console.WriteLine();
// Contains function.
bool val = qu.Contains('E');
Console.WriteLine("Check whether it contains 'E': ");
Console.WriteLine(val);
Console.ReadKey();
}
}
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved