Hashtable.Remove(Object) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Removes the element with the specified key from the 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)
- key
- Object
The key of the element to remove.
key
is null
.
The following example shows how to remove elements from the 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( "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 );
}
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: 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
*/
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
If the Hashtable does not contain an element with the specified key, the Hashtable remains unchanged. No exception is thrown.
This method is an O(1)
operation.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: