次の方法で共有


Stack.Clear メソッド

Stack からすべてのオブジェクトを削除します。

Public Overridable Sub Clear()
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

解説

Count は 0 に設定されます。

使用例

[Visual Basic, C#, C++] Stack の値を消去する方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        myStack.Push("jumped")
        
        ' Displays the count and values of the Stack.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myStack.Count)
        Console.Write("   Values:")
        PrintValues(myStack)
        
        ' Clears the Stack.
        myStack.Clear()
        
        ' Displays the count and values of the Stack.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myStack.Count)
        Console.Write("   Values:")
        PrintValues(myStack)
    End Sub    
    
    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab & "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Values:    jumped    fox    brown    quick    The
' After Clear,
'    Count    : 0
'    Values:
 

[C#] 
using System;
using System.Collections;

public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes a new Stack.
      Stack myStack = new Stack();
      myStack.Push( "The" );
      myStack.Push( "quick" );
      myStack.Push( "brown" );
      myStack.Push( "fox" );
      myStack.Push( "jumped" );

      // Displays the count and values of the Stack.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", myStack.Count );
      Console.Write( "   Values:" );
      PrintValues( myStack );

      // Clears the Stack.
      myStack.Clear();

      // Displays the count and values of the Stack.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", myStack.Count );
      Console.Write( "   Values:" );
      PrintValues( myStack );
   }


   public static void PrintValues( IEnumerable myCollection )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
   Count    : 5
   Values:    jumped    fox    brown    quick    The
After Clear,
   Count    : 0
   Values:
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


void PrintValues( IEnumerable* myCollection )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( S"\t{0}", myEnumerator->Current );
   Console::WriteLine();
}


int main()  {

   // Creates and initializes a new Stack.
   Stack* myStack = new Stack();
   myStack->Push( S"The" );
   myStack->Push( S"quick" );
   myStack->Push( S"brown" );
   myStack->Push( S"fox" );
   myStack->Push( S"jumped" );

   // Displays the count and values of the Stack.
   Console::WriteLine( S"Initially," );
   Console::WriteLine( S"   Count    : {0}", __box(myStack->Count) );
   Console::Write( S"   Values:" );
   PrintValues( myStack );

   // Clears the Stack.
   myStack->Clear();

   // Displays the count and values of the Stack.
   Console::WriteLine( S"After Clear," );
   Console::WriteLine( S"   Count    : {0}", __box(myStack->Count) );
   Console::Write( S"   Values:" );
   PrintValues( myStack );
}

/*
This code produces the following output.

Initially,
   Count    : 5
   Values:      jumped  fox     brown   quick   The
After Clear,
   Count    : 0
   Values:
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Stack クラス | Stack メンバ | System.Collections 名前空間