C# | Obtener el número de pares clave/valor en StringDictionary

La propiedad StringDictionary.Count se usa para obtener el número de pares clave/valor en el StringDictionary.

Sintaxis:

public virtual int Count { get; }

Valor devuelto: Devuelve el número de pares clave/valor en el StringDictionary.

Nota: Recuperar el valor de esta propiedad es una operación O(1).

Los siguientes programas ilustran el uso de la propiedad StringDictionary.Count :

Ejemplo 1:

// C# code to get the number of key/value
// pairs in the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Getting the number of key/value
        // pairs in the StringDictionary
        Console.Write("Number of key/value pairs in myDict are : ");
  
        Console.WriteLine(myDict.Count);
    }
}
Producción:

Number of key/value pairs in myDict are : 0

Ejemplo 2:

// C# code to get the number of key/value
// pairs in the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("G", "Geeks");
        myDict.Add("F", "For");
        myDict.Add("C", "C++");
        myDict.Add("DS", "Data Structures");
        myDict.Add("N", "Noida");
  
        // Getting the number of key/value
        // pairs in the StringDictionary
        Console.Write("Number of key/value pairs in myDict are : ");
  
        Console.WriteLine(myDict.Count);
    }
}
Producción:

Number of key/value pairs in myDict are : 5

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 *