Hashtable.Remove メソッド
指定したキーを持つ要素を Hashtable から削除します。
Public Overridable Sub Remove( _
ByVal key As Object _) Implements IDictionary.Remove
[C#]
public virtual void Remove(objectkey);
[C++]
public: virtual void Remove(Object* key);
[JScript]
public function Remove(
key : Object);
パラメータ
- key
削除する要素のキー。
実装
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | key が null 参照 (Visual Basic では Nothing) です。 |
NotSupportedException | Hashtable が読み取り専用です。
または Hashtable が固定サイズです。 |
解説
指定したキーを持つ要素が Hashtable に格納されていない場合、 Hashtable は変更されません。例外はスローされません。
使用例
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(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.
'
' 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
[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( "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 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.
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
*/
[C++]
#using <mscorlib.dll>
#using <system.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"1a", S"The" );
myHT->Add( S"1b", S"quick" );
myHT->Add( S"1c", S"brown" );
myHT->Add( S"2a", S"fox" );
myHT->Add( S"2b", S"jumped" );
myHT->Add( S"2c", S"over" );
myHT->Add( S"3a", S"the" );
myHT->Add( S"3b", S"lazy" );
myHT->Add( S"3c", S"dog" );
// Displays the Hashtable.
Console::WriteLine( S"The Hashtable initially contains the following:" );
PrintKeysAndValues( myHT );
// Removes the element with the key "3b".
myHT->Remove( S"3b" );
// Displays the current state of the Hashtable.
Console::WriteLine( S"After removing \"lazy\":" );
PrintKeysAndValues( myHT );
}
/*
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
*/
[JScript]
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 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 名前空間 | Add | IDictionary.Remove