Hashtable.Clear 메서드
Hashtable에서 요소를 모두 제거합니다.
네임스페이스: System.Collections
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
Public Overridable Sub Clear
‘사용 방법
Dim instance As Hashtable
instance.Clear
public virtual void Clear ()
public:
virtual void Clear ()
public void Clear ()
public function Clear ()
예외
예외 형식 | 조건 |
---|---|
Hashtable가 읽기 전용인 경우 |
설명
Count는 0으로 설정되고, 컬렉션 요소의 다른 개체에 대한 참조도 해제됩니다. 용량은 변경되지 않습니다.
이 메서드는 O(n) 연산이며, 여기서 n은 Count입니다.
예제
다음 예제에서는 Hashtable의 값을 지우는 방법을 설명합니다.
Imports System
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", "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 'Main
Public Shared Sub PrintKeysAndValues(myHT As Hashtable)
Console.WriteLine(vbTab + "-KEY-" + vbTab + "-VALUE-")
Dim de As DictionaryEntry
For Each de In myHT
Console.WriteLine(vbTab + "{0}:" + vbTab + "{1}", de.Key, de.Value)
Next de
Console.WriteLine()
End Sub 'PrintKeysAndValues
End Class 'SamplesHashtable
' This code produces the following output.
'
' Initially,
' Count : 5
' Values:
' -KEY- -VALUE-
' two: quick
' three: brown
' four: fox
' five: jumped
' 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.
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 myHT ) {
Console.WriteLine( "\t-KEY-\t-VALUE-" );
foreach ( DictionaryEntry de in myHT )
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: jumped
one: The
After Clear,
Count : 0
Values:
-KEY- -VALUE-
*/
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", "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 );
}
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: jumped
one: The
After Clear,
Count : 0
Values:
-KEY- -VALUE-
*/
import System.*;
import System.Collections.*;
public class SamplesHashtable
{
public static void main(String[] args)
{
// 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}",
System.Convert.ToString(myHT.get_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}",
System.Convert.ToString(myHT.get_Count()));
Console.WriteLine(" Values:");
PrintKeysAndValues(myHT);
} //main
public static void PrintKeysAndValues(Hashtable myHT)
{
Console.WriteLine("\t-KEY-\t-VALUE-");
IEnumerator myEnumerator = myHT.GetEnumerator();
while (myEnumerator.MoveNext()) {
DictionaryEntry de = (DictionaryEntry)myEnumerator.get_Current();
Console.WriteLine("\t{0}:\t{1}", de.get_Key(), de.get_Value());
}
Console.WriteLine();
} //PrintKeysAndValues
} //SamplesHashtable
/*
This code produces the following output.
Initially,
Count : 5
Values:
-KEY- -VALUE-
two: quick
three: brown
four: fox
five: jumped
one: The
After Clear,
Count : 0
Values:
-KEY- -VALUE-
*/
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 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0, 1.0에서 지원
참고 항목
참조
Hashtable 클래스
Hashtable 멤버
System.Collections 네임스페이스
IDictionary.Clear