
- 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
What is the Queue class in C#?
To represent a first-in first-out collection of objects in C#, use the Queue class. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque.
Some of the methods of the Queue class include.
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 how to work with enqueue and deque in C# that comes under the queue class.
Example
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { Queue q = new Queue(); q.Enqueue('A'); q.Enqueue('B'); q.Enqueue('C'); q.Enqueue('D'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); q.Enqueue('E'); q.Enqueue('F'); q.Enqueue('G'); q.Enqueue('H'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); Console.WriteLine("Removing some values "); char ch = (char)q.Dequeue(); Console.WriteLine("Value removed: {0}", ch); ch = (char)q.Dequeue(); Console.WriteLine("Value removed: {0}", ch); Console.ReadKey(); } } }
Output
Current queue: A B C D Current queue: A B C D E F G H Removing some values Value removed: A Value removed: B
Above, using enqueue the elements are added.
Queue q = new Queue(); q.Enqueue('A'); q.Enqueue('B'); q.Enqueue('C'); q.Enqueue('D');
The elements are deleted using dequeue.
char ch = (char)q.Dequeue(); Console.WriteLine("Value removed: {0}", ch);
- Related Articles
- What is the Count property of Queue class in C#?
- The Queue Class in Javascript
- How to use Queue class in C#?
- Enqueue and deque in Queue class in C#
- Create a queue using LinkedList class in Java
- What is difference between Microtask Queue and Callback Queue in asynchronous JavaScript?
- What is a Queue Automaton?
- What is the class "class" in Java?
- STL Priority Queue for Structure or Class in C++
- What is Heap queue (or heapq) in Python?
- What is Queue in Python? Explain with examples
- What is an in-memory Queue in Data Structures?
- What is the super class of every class in Java?
- What is the root class in Java?
- What is the object class in Java?

Advertisements