Hashtable.Add(Object, Object) Metodo

Definizione

Aggiunge un elemento con la chiave e il valore specificati in Hashtable.

public:
 virtual void Add(System::Object ^ key, System::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)

Parametri

key
Object

Chiave dell'elemento da aggiungere.

value
Object

Valore dell'elemento da aggiungere. Il valore può essere null.

Implementazioni

Eccezioni

key è null.

Un elemento con la stessa chiave esiste già in Hashtable.

è Hashtable di sola lettura.

oppure

Ha Hashtable una dimensione fissa.

Esempio

Nell'esempio seguente viene illustrato come aggiungere elementi all'oggetto 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("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
'

Commenti

Una chiave non può essere null, ma un valore può essere .

Un oggetto che non ha alcuna correlazione tra lo stato e il valore del codice hash deve in genere non essere usato come chiave. Ad esempio, gli oggetti String sono migliori rispetto agli oggetti StringBuilder da usare come chiavi.

È anche possibile utilizzare la Item[] proprietà per aggiungere nuovi elementi impostando il valore di una chiave che non esiste in Hashtable, ad esempio myCollection["myNonexistentKey"] = myValue. Tuttavia, se la chiave specificata esiste già in , l'impostazione Hashtabledella Item[] proprietà sovrascrive il valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti.

Se Count è minore della capacità di Hashtable, questo metodo è un'operazione O(1) . Se la capacità deve essere aumentata per contenere il nuovo elemento, questo metodo diventa un'operazione O(n) , dove n è Count.

Si applica a

Vedi anche