C# | Agregar la clave y el valor especificados en HybridDictionary – Part 1

El método HybridDictionary.Add(Object, Object) se usa para agregar una entrada con la clave y el valor especificados en HybridDictionary.

Sintaxis:

public void Add (object key, object value);

Parámetros:

clave : La clave de la entrada a agregar.
value : El valor de la entrada a agregar. El valor puede ser nulo.

Excepciones:

  • ArgumentNullException: si la clave es nula.
  • ArgumentException: si ya existe una entrada con la misma clave en HybridDictionary.

A continuación se dan algunos ejemplos para entender la implementación de una mejor manera:

Ejemplo 1:

// C# code to add an entry with
// the specified key and value
// into the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " --> " + de.Value);
        }
    }
}

Producción:

Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia --> Canberra
Belgium --> Brussels
Netherlands --> Amsterdam
China --> Beijing
Russia --> Moscow
India --> New Delhi

Ejemplo 2:

// C# code to add an entry with
// the specified key and value
// into the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " --> " + de.Value);
        }
    }
}

Producción:

Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
I --> first
II --> second
III --> third
IV --> fourth
V --> fifth

Nota:

  • Un objeto que no tiene correlación entre su estado y su valor de código hash normalmente no se debe usar como clave. Por ejemplo, los objetos String son mejores que los objetos StringBuilder para usarlos como claves.
  • Una clave no puede ser nula, pero un valor sí.
  • Este método es una operación O(1).
  • Cuando el número de elementos supera el tamaño óptimo para ListDictionary , los elementos se copian de ListDictionary a Hashtable . Sin embargo, esto solo sucede una vez. Si la colección ya está almacenada en Hashtable y la cantidad de elementos cae por debajo del tamaño óptimo para ListDictionary, la colección permanece en Hashtable.

Referencia:

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *