Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Collections.Generic.Queue: practical reference

System.Collections.Generic.Queue: practical reference: Queue<T> implements first-in, first-out order through Enqueue, Peek, and Dequeue.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Collections.Generic.Queue /* select a documented member or constructor */

Queue implements first-in, first-out order through Enqueue, Peek, and Dequeue.

Use it for work queues and breadth-first processing on one thread or with external synchronization.

It is not safe for concurrent writes; choose ConcurrentQueue when multiple threads modify the queue.

Dequeue and Peek on an empty queue throw InvalidOperationException, while TryDequeue and TryPeek return false.

The complete program actually uses System.Collections.Generic.Queue; its exact expected stdout is 1.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var queue = new Queue<int>();
queue.Enqueue(1); queue.Enqueue(2);
Console.WriteLine(queue.Dequeue());

Result

1

The complete program actually uses `System.Collections.Generic.Queue`; its exact expected stdout is `1`.

Sources