Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Fügt der Hashtable ein Element mit dem angegebenen Schlüssel und Wert hinzu.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Overridable Sub Add ( _
key As Object, _
value As Object _
)
'Usage
Dim instance As Hashtable
Dim key As Object
Dim value As Object
instance.Add(key, value)
public virtual void Add (
Object key,
Object value
)
public:
virtual void Add (
Object^ key,
Object^ value
)
public void Add (
Object key,
Object value
)
public function Add (
key : Object,
value : Object
)
Parameter
- key
Der Schlüssel des hinzuzufügenden Elements.
- value
Der Wert des hinzuzufügenden Elements. Der Wert kann NULL (Nothing in Visual Basic) sein.
Ausnahmen
| Ausnahmetyp | Bedingung |
|---|---|
key ist NULL (Nothing in Visual Basic). |
|
Ein Element mit demselben Schlüssel ist bereits in der Hashtable vorhanden. |
|
Die Hashtable ist schreibgeschützt. – oder – Die Hashtable hat eine feste Größe. |
Hinweise
Ein Schlüssel kann nicht NULL (Nothing in Visual Basic) sein, bei einem Wert ist dies hingegen möglich.
Wenn bei einem Objekt keine Korrelation zwischen seinem Zustand und seinem Hashcodewert besteht, sollte es nicht als Schlüssel verwendet werden. Beispielsweise sollten String-Objekte bei der Verwendung als Schlüssel StringBuilder-Objekten vorgezogen werden.
Sie können die Item-Eigenschaft auch zum Hinzufügen neuer Elemente verwenden, indem Sie den Wert eines Schlüssels festlegen, der in der Hashtable nicht vorhanden ist, beispielsweise myCollection["myNonexistentKey"] = myValue. Wenn der angegebene Schlüssel jedoch bereits in der Hashtable vorhanden ist, wird der alte Wert durch Festlegen der Item-Eigenschaft überschrieben. Im Gegensatz dazu ändert die Add-Methode keine vorhandenen Elemente.
Wenn Count kleiner als die Kapazität der Hashtable ist, ist diese Methode eine O(1)-Operation. Wenn die Kapazität zur Anpassung an das neue Element erhöht werden muss, wird diese Methode zu einer O(n)-Operation, wobei n gleich Count ist.
Beispiel
Im folgenden Beispiel wird gezeigt, wie der Hashtable Elemente hinzugefügt werden.
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")
' Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:")
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.
'
' The Hashtable contains the following:
' -KEY- -VALUE-
' two: quick
' three: brown
' four: fox
' one: 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( "one", "The" );
myHT.Add( "two", "quick" );
myHT.Add( "three", "brown" );
myHT.Add( "four", "fox" );
// Displays the Hashtable.
Console.WriteLine( "The Hashtable contains the following:" );
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.
The Hashtable contains the following:
-KEY- -VALUE-
two: quick
three: brown
four: fox
one: 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( "one", "The" );
myHT->Add( "two", "quick" );
myHT->Add( "three", "brown" );
myHT->Add( "four", "fox" );
// Displays the Hashtable.
Console::WriteLine( "The Hashtable contains the following:" );
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.
The Hashtable contains the following:
-KEY- -VALUE-
two: quick
three: brown
four: fox
one: 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("one", "The");
myHT.Add("two", "quick");
myHT.Add("three", "brown");
myHT.Add("four", "fox");
// Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:");
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.
The Hashtable contains the following:
-KEY- -VALUE-
two: quick
three: brown
four: fox
one: The
*/
import System
import System.Collections
// 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")
// Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:")
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 contains the following:
// -KEY- -VALUE-
// three: brown
// four: fox
// two: quick
// one: The
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
Hashtable-Klasse
Hashtable-Member
System.Collections-Namespace
Remove
Item
IDictionary.Add