C# | Obtenga una ICollection que contenga los valores en ListDictionary

La propiedad ListDictionary.Values ​​se usa para obtener una ICollection que contiene los valores en ListDictionary.

Sintaxis:

public System.Collections.ICollection Values { get; }

Valor de retorno: devuelve una ICollection que contiene los valores en ListDictionary.

A continuación se muestran los programas para ilustrar el uso de la propiedad ListDictionary.Values :

Ejemplo 1:

// C# code to get an ICollection
// containing the values in 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();
  
        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");
  
        // Get an ICollection containing
        // the values in the ListDictionary
        ICollection ic = myDict.Values;
  
        // Displaying the values in ICollection ic
        foreach(String str in ic)
        {
            Console.WriteLine(str);
        }
    }
}
Producción:

Canberra
Brussels
Amsterdam
Beijing
Moscow
New Delhi

Ejemplo 2:

// C# code to get an ICollection
// containing the values in 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();
  
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // Get an ICollection containing
        // the values in the ListDictionary
        ICollection ic = myDict.Values;
  
        // Displaying the values in ICollection ic
        foreach(String str in ic)
        {
            Console.WriteLine(str);
        }
    }
}
Producción:

first
second
third
fourth
fifth

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 ListDictionary original. Por lo tanto, los cambios en ListDictionary 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 *