다음을 통해 공유


Hashtable.Remove 메서드

Hashtable에서 지정한 키를 가지는 요소를 제거합니다.

네임스페이스: System.Collections
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Overridable Sub Remove ( _
    key As Object _
)
‘사용 방법
Dim instance As Hashtable
Dim key As Object

instance.Remove(key)
public virtual void Remove (
    Object key
)
public:
virtual void Remove (
    Object^ key
)
public void Remove (
    Object key
)
public function Remove (
    key : Object
)

매개 변수

  • key
    제거할 요소의 키입니다.

예외

예외 형식 조건

ArgumentNullException

key가 Null 참조(Visual Basic의 경우 Nothing)인 경우

NotSupportedException

Hashtable가 읽기 전용인 경우

- 또는 -

Hashtable의 크기가 고정되어 있는 경우

설명

Hashtable에 지정한 키를 가지는 요소가 없으면 Hashtable는 변경되지 않은 상태로 유지됩니다. 예외가 throw되지 않습니다.

이 메서드는 O(1) 연산입니다.

예제

다음 예제에서는 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("1a", "The")
        myHT.Add("1b", "quick")
        myHT.Add("1c", "brown")
        myHT.Add("2a", "fox")
        myHT.Add("2b", "jumped")
        myHT.Add("2c", "over")
        myHT.Add("3a", "the")
        myHT.Add("3b", "lazy")
        myHT.Add("3c", "dog")
        
        ' Displays the Hashtable.
        Console.WriteLine("The Hashtable initially contains the following:")
        PrintKeysAndValues(myHT)
        
        ' Removes the element with the key "3b".
        myHT.Remove("3b")
        
        ' Displays the current state of the Hashtable.
        Console.WriteLine("After removing ""lazy"":")
        PrintKeysAndValues(myHT)
    End Sub    
    
    
    Public Shared Sub PrintKeysAndValues(myHT As Hashtable)
        Dim de As DictionaryEntry
        For Each de In  myHT
            Console.WriteLine("    {0}:    {1}", de.Key, de.Value)
        Next de
        Console.WriteLine()
    End Sub
End Class


' This code produces the following output.
' 
'The Hashtable initially contains the following:
'    2c:    over
'    3a:    the
'    2b:    jumped
'    3b:    lazy
'    1b:    quick
'    3c:    dog
'    2a:    fox
'    1c:    brown
'    1a:    The
'
'After removing "lazy":
'    2c:    over
'    3a:    the
'    2b:    jumped
'    1b:    quick
'    3c:    dog
'    2a:    fox
'    1c:    brown
'    1a:    The
using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add( "1a", "The" );
      myHT.Add( "1b", "quick" );
      myHT.Add( "1c", "brown" );
      myHT.Add( "2a", "fox" );
      myHT.Add( "2b", "jumped" );
      myHT.Add( "2c", "over" );
      myHT.Add( "3a", "the" );
      myHT.Add( "3b", "lazy" );
      myHT.Add( "3c", "dog" );

      // Displays the Hashtable.
      Console.WriteLine( "The Hashtable initially contains the following:" );
      PrintKeysAndValues( myHT );

      // Removes the element with the key "3b".
      myHT.Remove( "3b" );

      // Displays the current state of the Hashtable.
      Console.WriteLine( "After removing \"lazy\":" );
      PrintKeysAndValues( myHT );
   }


   public static void PrintKeysAndValues( Hashtable myHT )  {
      foreach ( DictionaryEntry de in myHT )
         Console.WriteLine( "    {0}:    {1}", de.Key, de.Value );
      Console.WriteLine();
   }

}


/*
This code produces the following output.

The Hashtable initially contains the following:
    2c:    over
    3a:    the
    2b:    jumped
    3b:    lazy
    1b:    quick
    3c:    dog
    2a:    fox
    1c:    brown
    1a:    The

After removing "lazy":
    2c:    over
    3a:    the
    2b:    jumped
    1b:    quick
    3c:    dog
    2a:    fox
    1c:    brown
    1a:    The

*/
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( "1a", "The" );
   myHT->Add( "1b", "quick" );
   myHT->Add( "1c", "brown" );
   myHT->Add( "2a", "fox" );
   myHT->Add( "2b", "jumped" );
   myHT->Add( "2c", "over" );
   myHT->Add( "3a", "the" );
   myHT->Add( "3b", "lazy" );
   myHT->Add( "3c", "dog" );
   
   // Displays the Hashtable.
   Console::WriteLine( "The Hashtable initially contains the following:" );
   PrintKeysAndValues( myHT );
   
   // Removes the element with the key "3b".
   myHT->Remove( "3b" );
   
   // Displays the current state of the Hashtable.
   Console::WriteLine( "After removing \"lazy\":" );
   PrintKeysAndValues( myHT );
}

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

   Console::WriteLine();
}

/*
 This code produces the following output.
 
 The Hashtable initially contains the following:
     2c:    over
     3a:    the
     2b:    jumped
     3b:    lazy
     1b:    quick
     3c:    dog
     2a:    fox
     1c:    brown
     1a:    The

 After removing "lazy":
     2c:    over
     3a:    the
     2b:    jumped
     1b:    quick
     3c:    dog
     2a:    fox
     1c:    brown
     1a:    The

 */
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("1a", "The");
        myHT.Add("1b", "quick");
        myHT.Add("1c", "brown");
        myHT.Add("2a", "fox");
        myHT.Add("2b", "jumped");
        myHT.Add("2c", "over");
        myHT.Add("3a", "the");
        myHT.Add("3b", "lazy");
        myHT.Add("3c", "dog");

        // Displays the Hashtable.
        Console.WriteLine("The Hashtable initially contains the following:");
        PrintKeysAndValues(myHT);

        // Removes the element with the key "3b".
        myHT.Remove("3b");

        // Displays the current state of the Hashtable.
        Console.WriteLine("After removing \"lazy\":");
        PrintKeysAndValues(myHT);
    } //main

    public static void PrintKeysAndValues(Hashtable myHT)
    {
        IEnumerator myEnumerator = myHT.GetEnumerator();
        while (myEnumerator.MoveNext()) {
            DictionaryEntry de = (DictionaryEntry)myEnumerator.get_Current();
            Console.WriteLine("    {0}:    {1}", de.get_Key(), de.get_Value());
        }
        Console.WriteLine();
    } //PrintKeysAndValues
} //SamplesHashtable 

/*
 This code produces the following output.
 
 The Hashtable initially contains the following:
     2c:    over
     3a:    the
     2b:    jumped
     3b:    lazy
     1b:    quick
     3c:    dog
     2a:    fox
     1c:    brown
     1a:    The

 After removing "lazy":
     2c:    over
     3a:    the
     2b:    jumped
     1b:    quick
     3c:    dog
     2a:    fox
     1c:    brown
     1a:    The
 */
import System
import System.Collections
import Microsoft.VisualBasic

// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("1a", "The")
myHT.Add("1b", "quick")
myHT.Add("1c", "brown")
myHT.Add("2a", "fox")
myHT.Add("2b", "jumped")
myHT.Add("2c", "over")
myHT.Add("3a", "the")
myHT.Add("3b", "lazy")
myHT.Add("3c", "dog")

// Displays the Hashtable.
Console.WriteLine("The Hashtable initially contains the following:")
PrintKeysAndValues(myHT)

// Removes the element with the key "3b".
myHT.Remove("3b")

// Displays the current state of the Hashtable.
Console.WriteLine("After removing \"lazy\":")
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.
// 
// The Hashtable initially contains the following:
//     -KEY-    -VALUE-
//     3a:      the
//     3c:      dog
//     3b:      lazy
//     1c:      brown
//     1b:      quick
//     1a:      The
//     2a:      fox
//     2b:      jumped
//     2c:      over
// 
// After removing "lazy":
//     -KEY-    -VALUE-
//     3a:      the
//     3c:      dog
//     1c:      brown
//     1b:      quick
//     1a:      The
//     2a:      fox
//     2b:      jumped
//     2c:      over 

플랫폼

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 네임스페이스
Add
IDictionary.Remove