C# | Obtenga una colección de claves en el StringDictionary – Part 1

La propiedad StringDictionary.Keys se usa para obtener una colección de claves en StringDictionary.

Sintaxis:

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

Valor devuelto: una ICollection que proporciona las claves en StringDictionary.

A continuación se dan algunos ejemplos para entender la implementación de una mejor manera:

Ejemplo 1:

// C# code to get a collection
// of keys 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");
  
        // To copy the StringDictionary values to
        // a one-dimensional Array instance at
        // the specified index.
        String[] myKeys = new String[myDict.Count];
        myDict.Keys.CopyTo(myKeys, 0);
  
        // Getting a collection of keys
        // in the StringDictionary
        for (int i = 0; i < myDict.Count; i++) {
            Console.WriteLine(myKeys[i] + " " + myDict[myKeys[i]]);
        }
    }
}

Producción:

d Dog
b Banana
c Cat
a Apple

Ejemplo 2:

// C# code to get a collection
// of keys 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");
  
        // To copy the StringDictionary values to
        // a one-dimensional Array instance at
        // the specified index.
        String[] myKeys = new String[myDict.Count];
        myDict.Keys.CopyTo(myKeys, 0);
  
        // Getting a collection of keys
        // in the StringDictionary
        for (int i = 0; i < myDict.Count; i++) {
            Console.WriteLine(myKeys[i] + " " + myDict[myKeys[i]]);
        }
    }
}

Producción:

2 prime & even
3 prime & odd
9 non-prime & odd
4 non-prime & even

Nota:

  • No se especifica el orden de las claves en ICollection, pero es el mismo orden que los valores asociados en ICollection devueltos por el método Values .
  • La ICollection devuelta no es una copia estática. En su lugar, ICollection hace referencia a las claves 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 *