C# - How to Clear All Elements from Queue?

In this tutorial, we will learn how to clear all elements from the queue in C#? By IncludeHelp Last updated : March 28, 2023

Clear All Elements from Queue

To clear all from a queue, we use Queue.Clear() method. This method removes all elements from the queue.

Read More: C# Queue.Clear() Method

C# program to clear all elements from the 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);

      Q.Clear();

      Console.WriteLine("All items deleted successfully");
    }
  }
}

Output

All items deleted successfully

Explanation

In this program, we added 4 items into queue than delete all items using Clear() method.

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.