次の方法で共有


Hashtable.Clear メソッド

Hashtable からすべての要素を削除します。

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

実装

IDictionary.Clear

例外

例外の種類 条件
NotSupportedException Hashtable が読み取り専用です。

解説

Count は 0 に設定されます。容量は変更されません。

使用例

Hashtable の値を消去する方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesHashtable    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Hashtable.
        Dim myHT As New Hashtable()
        myHT.Add("one", "The")
        myHT.Add("two", "quick")
        myHT.Add("three", "brown")
        myHT.Add("four", "fox")
        myHT.Add("five", "jumped")
        
        ' Displays the count and values of the Hashtable.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myHT.Count)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(myHT)
        
        ' Clears the Hashtable.
        myHT.Clear()
        
        ' Displays the count and values of the Hashtable.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myHT.Count)
        Console.WriteLine("   Values:")
        PrintKeysAndValues(myHT)
    End Sub    
    
    Public Shared Sub PrintKeysAndValues(myList As Hashtable)
        Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
        Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
           + "-VALUE-")
        While myEnumerator.MoveNext()
            Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
               + "{1}", myEnumerator.Key, myEnumerator.Value)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Values:
'     -KEY-    -VALUE-
'     five:    jumped
'     three:    brown
'     four:    fox
'     two:    quick
'     one:    The
' 
' After Clear,
'    Count    : 0
'    Values:
'     -KEY-    -VALUE- 

[C#] 
using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add( "one", "The" );
      myHT.Add( "two", "quick" );
      myHT.Add( "three", "brown" );
      myHT.Add( "four", "fox" );
      myHT.Add( "five", "jumped" );

      // Displays the count and values of the Hashtable.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", myHT.Count );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( myHT );

      // Clears the Hashtable.
      myHT.Clear();

      // Displays the count and values of the Hashtable.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", myHT.Count );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( myHT );
   }


   public static void PrintKeysAndValues( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
   Count    : 5
   Values:
    -KEY-    -VALUE-
    five:    jumped
    three:    brown
    four:    fox
    two:    quick
    one:    The

After Clear,
   Count    : 0
   Values:
    -KEY-    -VALUE-
*/ 

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

using namespace System;
using namespace System::Collections;

void PrintKeysAndValues( Hashtable* myList )  {
   IDictionaryEnumerator* myEnumerator = myList->GetEnumerator();
   Console::WriteLine( S"\t-KEY-\t-VALUE-" );
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( S"\t{0}:\t{1}", myEnumerator->Key, myEnumerator->Value );
   Console::WriteLine();
}

void main()  {
   // Creates and initializes a new Hashtable.
   Hashtable* myHT = new Hashtable();
   myHT->Add( S"one", S"The" );
   myHT->Add( S"two", S"quick" );
   myHT->Add( S"three", S"brown" );
   myHT->Add( S"four", S"fox" );
   myHT->Add( S"five", S"jumped" );

   // Displays the count and values of the Hashtable.
   Console::WriteLine( S"Initially," );
   Console::WriteLine( S"   Count    : {0}", __box(myHT->Count) );
   Console::WriteLine( S"   Values:" );
   PrintKeysAndValues( myHT );

   // Clears the Hashtable.
   myHT->Clear();

   // Displays the count and values of the Hashtable.
   Console::WriteLine( S"After Clear," );
   Console::WriteLine( S"   Count    : {0}", __box(myHT->Count) );
   Console::WriteLine( S"   Values:" );
   PrintKeysAndValues( myHT );
}


 /*
 This code produces the following output.

 Initially,
    Count    : 5
    Values:
     -KEY-    -VALUE-
     five:    jumped
     three:    brown
     four:    fox
     two:    quick
     one:    The

 After Clear,
    Count    : 0
    Values:
     -KEY-    -VALUE-
 */

[JScript] 
import System
import System.Collections
import Microsoft.VisualBasic

// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("one", "The")
myHT.Add("two", "quick")
myHT.Add("three", "brown")
myHT.Add("four", "fox")
myHT.Add("five", "jumped")

// Displays the count and values of the Hashtable.
Console.WriteLine("Initially,")
Console.WriteLine("   Count    : {0}", myHT.Count)
Console.WriteLine("   Values:")
PrintKeysAndValues(myHT)

// Clears the Hashtable.
myHT.Clear()

// Displays the count and values of the Hashtable.
Console.WriteLine("After Clear,")
Console.WriteLine("   Count    : {0}", myHT.Count)
Console.WriteLine("   Values:")
PrintKeysAndValues(myHT)
    
function PrintKeysAndValues(myList : Hashtable){
    var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
    Console.WriteLine("\t-KEY-\t-VALUE-")
    while(myEnumerator.MoveNext())
        Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
    Console.WriteLine()
}

// This code produces the following output.
// 
// Initially,
//    Count    : 5
//    Values:
//     -KEY-    -VALUE-
//     five:    jumped
//     three:   brown
//     four:    fox
//     two:     quick
//     one:     The
// 
// After Clear,
//    Count    : 0
//    Values:
//     -KEY-    -VALUE- 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Hashtable クラス | Hashtable メンバ | System.Collections 名前空間 | IDictionary.Clear