Home >Backend Development >C#.Net Tutorial >Queue interface in C#

Queue interface in C#

PHPz
PHPzforward
2023-09-09 15:45:08750browse

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.

Example

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();
      }
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete