C# - Stack.Clear() Method with Example

Learn, how to clear all elements from stack in C#?
Submitted by IncludeHelp, on November 21, 2017 [Last updated : March 27, 2023]

Stack.Clear() Method

This is a method of 'Stack' class, it is used to clear/remove all elements of the stack.

Syntax

void Clear();

Parameter(s)

  • None

Return Value

It does not return any value.

C# program to clear elements of a stack

using System;
using System.Collections;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      Stack S = new Stack(5);

      S.Push(10);
      S.Push(20);
      S.Push(30);
      S.Push(40);

      S.Clear();

      Console.WriteLine("Now stack is empty");
    }
  }
}

Output

Now stack is empty

Note: In above program, to use 'Stack' 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.