
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to use Queue class in C#?
Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access to items. When you add an item to the list, it is called enqueue, and when you remove an item, it is called deque.
The following table lists some of the commonly used methods of the Queue class −
Sr.No | Method & Description |
---|---|
1 | public virtual void Clear(); Removes all elements from the Queue. |
2 | public virtual bool Contains(object obj); Determines whether an element is in the Queue. |
3 | public virtual object Dequeue(); Removes and returns the object at the beginning of the Queue. |
4 | public virtual void Enqueue(object obj); Adds an object to the end of the Queue. |
5 | public virtual object[] ToArray(); Copies the Queue to a new array. |
Let us see an example of the Queue class −
Example
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { Queue q = new Queue(); q.Enqueue('X'); q.Enqueue('Y'); q.Enqueue('Z'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); q.Enqueue('A'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); Console.ReadKey(); } } }
Output
Current queue: X Y Z Current queue: X Y Z A
- Related Articles
- What is the Queue class in C#?
- How to use Stack class in C#?
- How to use ArrayList class in C#?
- How to use NameValueCollection class in C#?
- Enqueue and deque in Queue class in C#
- How to use C# BinaryReader class?
- How to use C# BinaryWriter class?
- How to use C# FileStream class?
- How to use the directory class in C#?
- STL Priority Queue for Structure or Class in C++
- The Queue Class in Javascript
- What is the Count property of Queue class in C#?
- How to use ReadKey() method of Console class in C#?
- How to use WriteLine() method of Console class in C#?
- How to use the Clear method of array class in C#?

Advertisements