Hashtable.Add(Object, Object) Metoda

Definicja

Dodaje element z określonym kluczem i wartością do elementu Hashtable.

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)

Parametry

key
Object

Klucz elementu do dodania.

value
Object

Wartość elementu do dodania. Wartość może mieć wartość null.

Implementuje

Wyjątki

key to null.

Element o tym samym kluczu już istnieje w pliku Hashtable.

Element Hashtable jest tylko do odczytu.

-lub-

Element Hashtable ma stały rozmiar.

Przykłady

W poniższym przykładzie pokazano, jak dodać elementy do elementu 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
'

Uwagi

Klucz nie może być nullwartością , ale może to być wartość.

Obiekt, który nie ma korelacji między jego stanem a jego wartością kodu skrótu, zwykle nie powinien być używany jako klucz. Na przykład obiekty String są lepsze niż obiekty StringBuilder do użycia jako klucze.

Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w elemecie Hashtable, na przykład myCollection["myNonexistentKey"] = myValue. Jeśli jednak określony klucz już istnieje w Hashtableobiekcie , ustawienie Item[] właściwości zastępuje starą wartość. Z kolei Add metoda nie modyfikuje istniejących elementów.

Jeśli Count jest mniejsza niż pojemność Hashtableklasy , ta metoda jest operacją O(1) . Jeśli pojemność musi zostać zwiększona, aby pomieścić nowy element, ta metoda stanie się operacją O(n) , gdzie n to Count.

Dotyczy

Zobacz też