Queue

The Queue follows the FIFO principle, it means First In First Out which works as the first
added element to the Queue collection is available to remove first. It is like the items place in
an open-ended pipe.

It has the following methods:

- Enqueue() method to add the elements

- Dequeue () method to remove the elements

- Peek () method to read the last element.

Example 2: Queue collection adding and removing the element.

 

using System;
namespace SampleProgram {
  class MySample {
    public static void Main(string[] args) {
      Queue qu = new Queue();
      qu.Enqueue(100);
      qu.Enqueue(true);
      qu.Enqueue( " Hi ");
      qu.Enqueue(23.5);
      foreach(object m in qu) {
        Console.Write($ " {
          m
        } ");
      }
      Console.WriteLine();
      qu.Dequeue();
      foreach(object m in qu) {
        Console.Write($ " {
          m
        }");
      }
    }
  }
}

 

The output will be:

100 True Hi 23.5
True Hi 23.5
 

 

Related Tutorials