Hashtable.Remove(Object) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee verwijdert u het element met de opgegeven sleutel uit de Hashtable.
public:
virtual void Remove(System::Object ^ key);
public virtual void Remove(object key);
abstract member Remove : obj -> unit
override this.Remove : obj -> unit
Public Overridable Sub Remove (key As Object)
Parameters
- key
- Object
De sleutel van het element dat moet worden verwijderd.
Implementeringen
Uitzonderingen
key is null.
Voorbeelden
In het volgende voorbeeld ziet u hoe u elementen uit de Hashtable.
using System;
using System.Collections;
public class SamplesHashtable
{
public static void Main()
{
// Creates and initializes a new Hashtable.
var myHT = new Hashtable();
myHT.Add("1a", "The");
myHT.Add("1b", "quick");
myHT.Add("1c", "brown");
myHT.Add("2a", "fox");
myHT.Add("2b", "jumps");
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($" {de.Key}: {de.Value}");
Console.WriteLine();
}
}
/*
This code produces the following output.
The Hashtable initially contains the following:
2c: over
3a: the
2b: jumps
3b: lazy
1b: quick
3c: dog
2a: fox
1c: brown
1a: The
After removing "lazy":
2c: over
3a: the
2b: jumps
1b: quick
3c: dog
2a: fox
1c: brown
1a: The
*/
Imports System.Collections
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", "jumps")
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)
For Each de As DictionaryEntry In myHT
Console.WriteLine($" {de.Key}: {de.Value}")
Next
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
'The Hashtable initially contains the following:
' 1a: The
' 2c: over
' 3c: dog
' 1c: brown
' 2b: jumps
' 3b: lazy
' 1b: quick
' 2a: fox
' 3a: the
'After removing "lazy":
' 1a: The
' 2c: over
' 3c: dog
' 1c: brown
' 2b: jumps
' 1b: quick
' 2a: fox
' 3a: the
Opmerkingen
Als het Hashtable geen element met de opgegeven sleutel bevat, blijft de Hashtable waarde ongewijzigd. Er wordt geen uitzondering opgeworpen.
Deze methode is een O(1) bewerking.