Cómo crear un ListDictionary en C#

El constructor ListDictionary() se usa para inicializar una nueva instancia vacía de la clase ListDictionary usando el comparador predeterminado. ListDictionary es una colección especializada. Viene bajo el espacio de System.Collections.Specializednombres. Este tipo representa un tipo de diccionario no genérico. Se implementa con una lista enlazada. Esta clase es una implementación simple de una colección de diccionarios ( System.Collections.IDictionary) para listas pequeñas.

Sintaxis:

public ListDictionary ();

Ejemplo 1:

// C# Program to illustrate how
// to create a ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // ld is the ListDictionary object
        // ListDictionary() is the constructor
        // used to initializes a new
        // instance of the ListDictionary class
        ListDictionary ld = new ListDictionary();
  
        // Count property is used to get the
        // number of elements in ListDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine("The number of elements: "+ 
                                           ld.Count);
    }
}
Producción:

The number of elements: 0

Ejemplo 2:

// C# Program to illustrate how
// to create a ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // ld is the ListDictionary object
        // ListDictionary() is the constructor
        // used to initializes a new
        // instance of the ListDictionary class
        ListDictionary ld = new ListDictionary();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in ListDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(ld.Count);
  
        // Adding key/value pairs in ld
        ld.Add("Australia", "Canberra");
        ld.Add("Belgium", "Brussels");
        ld.Add("Netherlands", "Amsterdam");
        ld.Add("China", "Beijing");
        ld.Add("Russia", "Moscow");
        ld.Add("India", "New Delhi");
  
        Console.Write("After Add Method: ");
  
        // Count property is used to get the
        // number of elements in ld
        Console.WriteLine(ld.Count);
    }
}
Producción:

Before Add Method: 0
After Add Method: 6

Referencia:

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal 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 *