C# | Obtener un objeto IDictionaryEnumerator en OrderedDictionary – Part 1

El método OrderedDictionary.GetEnumerator devuelve un objeto IDictionaryEnumerator que itera a través de la colección OrderedDictionary.

Sintaxis:

public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();

Valor devuelto: un objeto IDictionaryEnumerator para la colección OrderedDictionary.

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

Ejemplo 1:

// C# code to get an IDictionaryEnumerator
// object that iterates through the
// OrderedDictionary collection.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
        myDict.Add("key3", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // To Get an IDictionaryEnumerator object
        // that iterates through the OrderedDictionary
        // collection.
        IDictionaryEnumerator myEnumerator = myDict.GetEnumerator();
  
        while (myEnumerator.MoveNext()) {
            Console.WriteLine(myEnumerator.Key + " --> " 
                                  + myEnumerator.Value);
        }
    }
}

Producción:

key1 --> value1
key2 --> value2
key3 --> value3
key4 --> value4
key5 --> value5

Ejemplo 2:

// C# code to get an IDictionaryEnumerator
// object that iterates through the
// OrderedDictionary collection.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
  
        // To Get an IDictionaryEnumerator object
        // that iterates through the OrderedDictionary
        // collection.
        IDictionaryEnumerator myEnumerator = myDict.GetEnumerator();
  
        while (myEnumerator.MoveNext()) {
            Console.WriteLine(myEnumerator.Key + " --> " 
                                 + myEnumerator.Value);
        }
    }
}

Producción:

A --> Apple
B --> Banana
C --> Cat
D --> Dog

Nota:

  • Los enumeradores se pueden usar para leer los datos de la colección, pero no se pueden usar para modificar la colección subyacente.
  • Inicialmente, el enumerador se coloca antes del primer elemento de la colección.
  • Este método es una operación O(1).
  • Un enumerador sigue siendo válido mientras la colección permanezca sin cambios. Si se realizan cambios en la colección, como agregar, modificar o eliminar elementos, el enumerador se invalida irremediablemente y su comportamiento no está definido.

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 *