C# - How to Insert Elements to Queue?

In this tutorial, we will learn how to insert elements to queue in C#?
Submitted by IncludeHelp, on November 23, 2017 [Last updated : March 28, 2023]

Insert Elements to Queue

To insert elements to queue, we use Queue.Enqueue() method. This method inserts the given element to queue.

Read More: C# Queue.Enqueue() Method

C# program to insert elements to queue

using System;
using System.Collections;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      Queue Q = new Queue(5);

      Q.Enqueue(10);
      Q.Enqueue(20);
      Q.Enqueue(30);
      Q.Enqueue(40);

      Console.WriteLine("Items are added or enqueued into Queue.");
    }
  }
}

Output

Items are added or enqueued into Queue.

In this program, we are adding 4 items into queue that 10,20,30,40 respectively by creating object of Queue class.

Note: In above program, to use 'Queue' class, we need to include System.Collection namespace.

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.