Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में List के माध्यम से इटरेट करने के लिए एन्यूमरेटर प्राप्त करें


List के माध्यम से इटरेट करने के लिए एन्यूमरेटर प्राप्त करने के लिए, कोड इस प्रकार है:

उदाहरण

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(String[] args){ 
        List list1 = new List(); 
        list1.Add("One"); 
        list1.Add("Two"); 
        list1.Add("Three"); 
        list1.Add("Four"); 
        list1.Add("Five"); 
        Console.WriteLine("Elements in List1..."); 
        foreach (string res in list1){ 
            Console.WriteLine(res); 
        } 
        List list2 = new List(); 
        list2.Add("India"); 
        list2.Add("US"); 
        list2.Add("UK"); 
        list2.Add("Canada"); 
        list2.Add("Poland"); 
        list2.Add("Netherlands"); 
        Console.WriteLine("Elements in List2..."); 
        List.Enumerator demoEnum = list2.GetEnumerator(); 
        while (demoEnum.MoveNext()) { 
            string res = demoEnum.Current; 
            Console.WriteLine(res); 
        } 
        Console.WriteLine("Is List2 equal to List1? = " + list2.Equals(list1)); 
    } 
}

आउटपुट

यह निम्न आउटपुट उत्पन्न करेगा:

Elements in List1... 
One 
Two 
Three 
Four 
Five 
Elements in List2... 
India 
US 
UK 
Canada 
Poland 
Netherlands 
Is List2 equal to List1? = False

उदाहरण

आइए एक और उदाहरण देखें:

using System; 
using System.Collections.Generic; 
public class Demo { 
    public static void Main(String[] args){ 
        List list = new List(); 
        list.Add("One"); 
        list.Add("Two"); 
        list.Add("Three"); 
        list.Add("Four"); 
        list.Add("Five"); 
        list.Add("Six"); 
        list.Add("Seven"); 
        list.Add("Eight"); 
        list.Add("Nine"); 
        list.Add("Ten"); 
        Console.WriteLine("Enumerator iterates through the list elements..."); 
        List.Enumerator demoEnum = list.GetEnumerator(); 
        while (demoEnum.MoveNext()) { 
            string res = demoEnum.Current; 
            Console.WriteLine(res); 
        } 
    } 
}

आउटपुट

यह निम्न आउटपुट उत्पन्न करेगा:

Enumerator iterates through the list elements... 
One 
Two 
Three 
Four 
Five 
Six 
Seven 
Eight 
Nine 
Ten
  1. - C# प्रोग्राम: सूची से पहले तीन तत्व प्राप्त करना

    C# में व्यक्तिगत संख्या में पहले तत्व प्राप्त करने के लिए Take() मेथड का उपयोग करें।सबसे पहले, एक सूची सेट करें और तत्व जोड़ें:List<string> myList = new List<string>(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four");

  1. - C# में List.GetRange() से तत्वों की रेंज प्राप्त करना

    C# में List<T>.GetRange() मेथड का उपयोग करके सूची के एक सन्निकट (contiguous) भाग को एक नई सूची के रूप में प्राप्त किया जा सकता है। यह ArraySegment या LINQ के Skip()/Take() के समान है लेकिन सूची-विशिष्ट है। उदाहरण using System; using System.Collections.Generic; public class Demo { public

  1. - C# में Dictionary से कुंजियों (Keys) की सूची प्राप्त करना

    C# में Dictionary<TKey, TValue> से सभी कुंजियों (keys) को एक सूची (List<TKey>) के रूप में प्राप्त करने के लिए Keys प्रॉपर्टी का उपयोग करके कंस्ट्रक्टर या ToList() मेथड का उपयोग किया जाता है। उदाहरण using System; using System.Collections.Generic; public class Demo { public static v