Hashtable.Add(Object, Object) Methode

Definition

Fügt dem Hashtable ein Element mit dem angegebenen Schlüssel und Wert hinzu.

public:
 virtual void Add(System::Object ^ key, System::Object ^ value);
public virtual void Add (object key, object value);
public virtual void Add (object key, object? value);
abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unit
Public Overridable Sub Add (key As Object, value As Object)

Parameter

key
Object

Der Schlüssel des hinzuzufügenden Elements.

value
Object

Der Wert des hinzuzufügenden Elements. Der Wert kann null sein.

Implementiert

Ausnahmen

key ist null.

In Hashtable ist bereits ein Element mit demselben Schlüssel enthalten.

Hashtable ist schreibgeschützt.

- oder -

Hashtable hat eine feste Größe.

Beispiele

Im folgenden Beispiel wird gezeigt, wie Elemente zu hinzugefügt werden 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( "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
 */
using System;
using System.Collections;
public class SamplesHashtable
{

   public static void Main()
   {
      // Creates and initializes a new Hashtable.
      var 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{de.Key}:\t{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
*/
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

    Public Shared Sub PrintKeysAndValues(myHT As Hashtable)
        Console.WriteLine(vbTab + "-KEY-" + vbTab + "-VALUE-")
        For Each de As DictionaryEntry In myHT
            Console.WriteLine(vbTab + "{0}:" + vbTab + "{1}", de.Key, de.Value)
        Next
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' The Hashtable contains the following:
'         -KEY-   -VALUE-
'         two:    quick
'         one:    The
'         three:  brown
'         four:   fox
'

Hinweise

Ein Schlüssel kann nicht sein null, aber ein Wert kann sein.

Ein Objekt, das keine Korrelation zwischen seinem Zustand und seinem Hashcodewert aufweist, sollte normalerweise nicht als Schlüssel verwendet werden. String-Objekte sind beispielsweise besser als StringBuilder-Objekte für die Verwendung als Schlüssel.

Sie können auch die Item[] zum Hinzufügen neuer Elemente durch Festlegen des Werts eines Schlüssels, die Eigenschaft ist nicht vorhanden, der Hashtable, z. B. myCollection["myNonexistentKey"] = myValue Wenn der angegebene Schlüssel jedoch bereits in vorhanden ist, überschreibt die HashtableItem[] -Eigenschaft den alten Wert. Im Gegensatz dazu ändert die Add -Methode vorhandene Elemente nicht.

Wenn Count die Kapazität von Hashtablekleiner ist, ist diese Methode ein O(1) Vorgang. Wenn die Kapazität erhöht werden muss, um das neue Element aufzunehmen, wird diese Methode zu einem O(n) Vorgang, wobei n ist Count.

Gilt für:

Weitere Informationen