C# | Eliminar la entrada con la clave especificada de ListDictionary

El método ListDictionary.Remove(Object) se usa para eliminar la entrada con la clave especificada del ListDictionary.

Sintaxis:

public void Remove (object key);

Aquí, clave es la clave de la entrada que se va a eliminar.

Excepción: este método dará ArgumentNullException si la clave es nula.

A continuación se muestran los ejemplos para ilustrar el uso del método ListDictionary.Remove(Object) :

Ejemplo 1:

// C# code to remove the entry with
// the specified key from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // 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);
        }
  
        // Remove the entry with the specified
        // key from the ListDictionary
        myDict.Remove("Russia");
  
        // 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
Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
India New Delhi

Ejemplo 2:

// C# code to remove the entry with
// the specified key from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // 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);
        }
  
        // Remove the entry with the specified
        // key from the ListDictionary
        // This should raise "ArgumentNullException"
        // as the key is null
        myDict.Remove(null);
  
        // 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);
        }
    }
}

Error de tiempo de ejecución:

Excepción no controlada:
System.ArgumentNullException: la clave no puede ser nula.
Nombre del parámetro: clave

Nota:

  • Si ListDictionary no contiene un elemento con la clave especificada, ListDictionary permanece sin cambios. No se lanza ninguna excepción.
  • Este método es una operación O(n), donde n es Count.

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 *