C# | Obtener una colección de valores en StringDictionary

La propiedad StringDictionary.Values ​​se usa para obtener una colección de valores en StringDictionary.

Sintaxis:

public virtual System.Collections.ICollection Values { get; }

Valor devuelto: una ICollection que proporciona los valores en StringDictionary.

Ejemplo 1:

// C# code to get a collection
// of values 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("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
  
        // Getting a collection of values
        // in the StringDictionary
        foreach(string val in myDict.Values)
        {
            Console.WriteLine(val);
        }
    }
}

Producción:

Dog
Banana
Cat
Apple

Ejemplo 2:

// C# code to get a collection
// of values 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("3", "prime & odd");
        myDict.Add("2", "prime & even");
        myDict.Add("4", "non-prime & even");
        myDict.Add("9", "non-prime & odd");
  
        // Getting a collection of values
        // in the StringDictionary
        foreach(string val in myDict.Values)
        {
            Console.WriteLine(val);
        }
    }
}

Producción:

prime & even
prime & odd
non-prime & odd
non-prime & even

Nota:

  • El orden de los valores en ICollection no está especificado, pero es el mismo orden que las claves asociadas en ICollection devueltas por el método Keys .
  • La ICollection devuelta no es una copia estática. En su lugar, ICollection hace referencia a los valores del StringDictionary original. Por lo tanto, los cambios en StringDictionary continúan reflejándose en ICollection.
  • Recuperar el valor de esta propiedad es una operación O(1).

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 *