C# Stack Clear

Description

Stack Clear removes all objects from the Stack.

Syntax

Stack.Clear has the following syntax.


public void Clear()

Example


using System;//from w  ww. j  av a2  s .  c  o  m
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Stack<string> numbers = new Stack<string>();
        numbers.Push("one");
        numbers.Push("two");
        numbers.Push("three");
        numbers.Push("four");
        numbers.Push("five");

        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }

        numbers.Clear();
        Console.WriteLine("\nstack2.Count = {0}", numbers.Count);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack