La clase Hashtable representa una colección de pares de clave y valor que se organizan según el código hash de la clave. La clave se utiliza para acceder a los elementos de la colección. El método Hashtable.Remove(Object) se usa para eliminar el elemento con la clave especificada de Hashtable.
Sintaxis:
public virtual void Remove (object key);
Parámetro:
key: Es la clave del elemento a remover de tipo System.Object .
Excepciones:
- ArgumentNullException: si la clave es nula.
- NotSupportedException: si Hashtable es de solo lectura o tiene un tamaño fijo.
Ejemplo:
// C# code to remove the element // with the specified key from Hashtable using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("2", "Even & Prime"); myTable.Add("3", "Odd & Prime"); myTable.Add("4", "Even & non-prime"); myTable.Add("9", "Odd & non-prime"); // Print the number of entries in Hashtable Console.WriteLine("Total number of entries in Hashtable : " + myTable.Count); // To remove the elements from Hashtable // which has key as "3" myTable.Remove("3"); // Print the number of entries in Hashtable Console.WriteLine("Total number of entries in Hashtable : " + myTable.Count); // To remove the elements from Hashtable // which has key as "4" myTable.Remove("4"); // Print the number of entries in Hashtable Console.WriteLine("Total number of entries in Hashtable : " + myTable.Count); // Adding elements in Hashtable myTable.Add("g", "geeks"); myTable.Add("c", "c++"); myTable.Add("d", "data structures"); // Print the number of entries in Hashtable Console.WriteLine("Total number of entries in Hashtable : " + myTable.Count); // To remove the elements from Hashtable // which has key as "c" myTable.Remove("c"); // Print the number of entries in Hashtable Console.WriteLine("Total number of entries in Hashtable : " + myTable.Count); } }
Producción:
Total number of entries in Hashtable : 4 Total number of entries in Hashtable : 3 Total number of entries in Hashtable : 2 Total number of entries in Hashtable : 5 Total number of entries in Hashtable : 4
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