Home >Backend Development >C#.Net Tutorial >Queue interface in C#
The queue represents a first-in, first-out collection of objects. Use this when you need first-in-first-out access to items. When you add an item to the list, it's called enqueuing, and when you remove an item, it's called deque.
Let's look at an example of the Queue class.
To add an element, use Enqueue -
Queue q = new Queue(); q.Enqueue('X'); q.Enqueue('Y'); q.Enqueue('Z');
To remove an element, use Dequeue -
// remove elements while (q.Count > 0) Console.WriteLine(q.Dequeue());
Let’s look at an example of adding an element in a queue.
Real-time demonstration
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { Queue q = new Queue(); q.Enqueue('t'); q.Enqueue('u'); q.Enqueue('v'); q.Enqueue('w'); q.Enqueue('x'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); Console.ReadKey(); } } }
Current queue: t u v w x
The above is the detailed content of Queue interface in C#. For more information, please follow other related articles on the PHP Chinese website!