Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Queue


Share

Queue

Think of a line of people at a shop. The first person in the line is served first, and new people join at the end of the line.

A Queue in C# works the same way. This is called first in, first out (FIFO).

A printer works like this too. When you send many documents to a printer, it prints them in the order they arrived.

A queue is the opposite of a Stack, where the last item in is the first item out.

Like List, Queue is in the System.Collections.Generic namespace:

Example

// Include the namespace that holds Queue
using System.Collections.Generic;

Enqueue and Peek

Enqueue() adds an item to the end of the queue. Peek() returns the first item, without removing it:

Example

Queue<string> customers = new Queue<string>();
customers.Enqueue("Liam");
customers.Enqueue("Jenny");
customers.Enqueue("Angie");

Console.WriteLine(customers.Peek());  // Liam

Try it Yourself »

Liam came first, so he is first in the queue.


Dequeue Items

Dequeue() removes the first item and returns it. Count tells you how many items are left:

Example

Queue<string> customers = new Queue<string>();
customers.Enqueue("Liam");
customers.Enqueue("Jenny");
customers.Enqueue("Angie");

Console.WriteLine(customers.Dequeue());  // Liam
Console.WriteLine(customers.Peek());     // Jenny
Console.WriteLine(customers.Count);      // 2

Try it Yourself »



Loop Through a Queue

A foreach loop goes through the queue from the first item to the last. It does not remove any items:

Example

Queue<string> customers = new Queue<string>();
customers.Enqueue("Liam");
customers.Enqueue("Jenny");
customers.Enqueue("Angie");

foreach (string customer in customers)
{
  Console.WriteLine(customer);
}

Try it Yourself »


Example: Print Jobs

This program puts three documents in a printer queue. Then it prints them one by one, until the queue is empty:

Example

Queue<string> printJobs = new Queue<string>();
printJobs.Enqueue("report.pdf");
printJobs.Enqueue("photo.jpg");
printJobs.Enqueue("letter.docx");

while (printJobs.Count > 0)
{
  Console.WriteLine("Printing " + printJobs.Dequeue());
}

Try it Yourself »

The documents are printed in the same order as they were added. Each Dequeue() removes one document, and the loop stops when Count is 0.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.