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

Queue interface in C#

PHPz
Release: 2023-09-09 15:45:08
forward
772 people have browsed it

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');
Copy after login

To remove an element, use Dequeue -

// remove elements
while (q.Count > 0)
Console.WriteLine(q.Dequeue());
Copy after login

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

Output

Current queue:
t u v w x
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template