• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Queue Class in C#

January 25, 2022 C#

In this post, you will learn how to work with the Queue class so that you can use that same knowledge and thus evolve.

  • this class is basically a queue (due to the way it works) and represents a FIFO (First In - First Out) type collection, that is, the first value entered will always be the first one to leave;
  • these values that make up a queue are called elements.
Queue (object).Enqueue() Adds an object at the bottom of the queue
Queue (object).Dequeue() Remove and return the object at the top of the queue
Queue (object).Peek() Return the object at the top of the queue without removing it
using System;
using System.Collections.Generic;

namespace Base {
  class QueueClass {
    private Queue<int> queue = new Queue<int>();
    private const int Multiplier = 10;
    private int number = 1;

    public void Run() {
      for (int index = 1; index <= 5; index++) {
        queue.Enqueue(number);
        number *= Multiplier;
      }

      PrintQueue();

      Console.WriteLine($"\nRemoving the number {queue.Dequeue()} from the queue with the Dequeue() method.\n");

      PrintQueue();

      Console.WriteLine($"\nThe number at the top of the queue is {queue.Peek()}.");
    }

    private void PrintQueue() {
      foreach (var number in queue)
        Console.WriteLine(number);
    }
  }
}

/*
  1
  10
  100
  1000
  10000

  Removing number 1 from the queue with the Dequeue() method.

  10
  100
  1000
  10000

  The number at the top of the queue is 10.
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved