다음을 통해 공유


Hashtable.Clear 메서드

정의

Hashtable에서 요소를 모두 제거합니다.

public:
 virtual void Clear();
public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

구현

예외

Hashtable이 읽기 전용인 경우

예제

다음 예제에서는 의 Hashtable값을 지우는 방법을 보여줍니다.

using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( Hashtable^ myHT );
int main()
{
   
   // Creates and initializes a new Hashtable.
   Hashtable^ myHT = gcnew Hashtable;
   myHT->Add( "one", "The" );
   myHT->Add( "two", "quick" );
   myHT->Add( "three", "brown" );
   myHT->Add( "four", "fox" );
   myHT->Add( "five", "jumps" );
   
   // 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 );
}

void PrintKeysAndValues( Hashtable^ myHT )
{
   Console::WriteLine( "\t-KEY-\t-VALUE-" );
   IEnumerator^ myEnum = myHT->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DictionaryEntry de = *safe_cast<DictionaryEntry ^>(myEnum->Current);
      Console::WriteLine( "\t{0}:\t{1}", de.Key, de.Value );
   }

   Console::WriteLine();
}

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

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

 */
using System;
using System.Collections;
public class SamplesHashtable
{

   public static void Main()
   {
      // Creates and initializes a new Hashtable.
      var myHT = new Hashtable();
      myHT.Add("one", "The");
      myHT.Add("two", "quick");
      myHT.Add("three", "brown");
      myHT.Add("four", "fox");
      myHT.Add("five", "jumps");

      // Displays the count and values of the Hashtable.
      Console.WriteLine("Initially,");
      Console.WriteLine($"   Count    : {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    : {myHT.Count}");
      Console.WriteLine("   Values:" );
      PrintKeysAndValues(myHT);
   }

   public static void PrintKeysAndValues( Hashtable myHT )
   {
      Console.WriteLine("\t-KEY-\t-VALUE-");
      foreach (DictionaryEntry de in myHT)
         Console.WriteLine("\t{de.Key}:\t{de.Value}");
      Console.WriteLine();
   }
}


/*
This code produces the following output.

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

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

*/
Imports System.Collections

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", "jumps")

        ' Displays the count and values of the Hashtable.
        Console.WriteLine("Initially,")
        Console.WriteLine($"   Count    : {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    : {myHT.Count}")
        Console.WriteLine("   Values:")
        PrintKeysAndValues(myHT)

    End Sub


    Public Shared Sub PrintKeysAndValues(myHT As Hashtable)
        Console.WriteLine(vbTab + "-KEY-" + vbTab + "-VALUE-")
        For Each de As DictionaryEntry In myHT
            Console.WriteLine(vbTab + "{0}:" + vbTab + "{1}", de.Key, de.Value)
        Next
        Console.WriteLine()
    End Sub

End Class


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

설명

Count가 0으로 설정되어 있고 컬렉션 요소에서 다른 개체로의 참조도 해제되어 있습니다. 용량은 변경되지 않은 상태로 유지됩니다.

이 메서드는 작업입니다 O(n) . 여기서 n 는 입니다 Count.

적용 대상

추가 정보