Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# Queues: Enqueue, Dequeue, and First‑In‑First‑Out Operations Explained

What is a Queue in C#?

A Queue implements the First‑In‑First‑Out (FIFO) principle, much like a line of people waiting for a bus. The first element added is the first one to be processed or removed.

In C#, queues are defined in the System.Collections namespace and offer methods such as Enqueue and Dequeue that mirror the behaviour of real‑world queues.

Declaring a Queue

Instantiate a queue using the Queue type and the new keyword:

Queue qt = new Queue();

Adding Elements (Enqueue)

Use Enqueue to append an item to the back of the queue:

qt.Enqueue(1);

Removing Elements (Dequeue)

The Dequeue method removes and returns the item at the front of the queue:

var first = qt.Dequeue();

Useful Properties and Methods

Practical Example

The following console application demonstrates queue creation, iteration, and the use of Count and Contains:

using System;
using System.Collections;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue qt = new Queue();
            qt.Enqueue(1);
            qt.Enqueue(2);
            qt.Enqueue(3);

            foreach (Object obj in qt)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine();
            Console.WriteLine("Total items: " + qt.Count);
            Console.WriteLine("Contains 3? " + qt.Contains(3));
            Console.ReadKey();
        }
    }
}

Output:

Mastering C# Queues: Enqueue, Dequeue, and First‑In‑First‑Out Operations Explained
Queue C# example

Removing the First Element

Calling Dequeue removes the earliest enqueued item. The following snippet shows the effect:

qt.Dequeue();
foreach (Object obj in qt)
{
    Console.WriteLine(obj);
}

Result:

Mastering C# Queues: Enqueue, Dequeue, and First‑In‑First‑Out Operations Explained
C# Queue Dequeue example

Summary

A C# Queue adheres to FIFO semantics. Use Enqueue to add items, Dequeue to remove the front item, and properties like Count and methods such as Contains to inspect the queue.

C Language

  1. P‑F Curve Explained: Mastering Reliability‑Centred Maintenance
  2. Breakdown Maintenance Explained: Rapid Response & Cost Control
  3. Mastering Java's String compareTo() Method: Syntax, Use Cases, and Practical Examples
  4. Master Rotary Encoders with Arduino: How They Work & Step‑by‑Step Integration
  5. Mastering the Raspberry Pi Camera Pinout: A Complete Guide to Setup and Usage
  6. PIC18 Microcontrollers: Features, Performance, and How to Use Them
  7. Water Flow Sensors: How They Work & How to Use Them
  8. Arduino SD Card 101: Setup, Connection, and Usage Guide
  9. Understanding Reference Designators: How to Label PCB Assembly Connections
  10. Community Cloud Explained: Benefits, Examples, and Real-World Use Cases