C# में ArrayList में तत्वों की एक विशिष्ट रेंज पर पुनरावृति करने वाला एन्यूमरेटर प्राप्त करने के लिए, हम GetEnumerator(int index, int count) ओवरलोड का उपयोग करते हैं। यह निर्दिष्ट इंडेक्स से शुरू होकर निर्दिष्ट संख्या में तत्वों के लिए एक एन्यूमरेटर लौटाता है। ArrayList System.Collections नेमस्पेस में स्थित एक गैर-जेनेरिक गतिशील सरणी है।
उदाहरण 1: इंडेक्स 1 से 3 तत्वों के लिए एन्यूमरेटर
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
ArrayList arrList = new ArrayList();
arrList.Add(100);
arrList.Add(200);
arrList.Add(300);
arrList.Add(400);
arrList.Add(500);
Console.WriteLine("एक रेंज में तत्व प्रदर्शित करें...");
IEnumerator demoEnum = arrList.GetEnumerator(1, 3);
while (demoEnum.MoveNext())
{
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
}
}
आउटपुट
एक रेंज में तत्व प्रदर्शित करें... 200 300 400
उदाहरण 2: स्ट्रिंग ArrayList पर एकाधिक रेंज
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
ArrayList arrList = new ArrayList();
arrList.Add("Andy");
arrList.Add("Katie");
arrList.Add("Taylor");
arrList.Add("Steve");
arrList.Add("Nathan");
arrList.Add("Carl");
arrList.Add("Mark");
arrList.Add("Amy");
arrList.Add("Jacob");
arrList.Add("John");
Console.WriteLine("एक रेंज में तत्व प्रदर्शित करें... (इंडेक्स 1 से 3 तत्व)");
IEnumerator demoEnum = arrList.GetEnumerator(1, 3);
while (demoEnum.MoveNext())
{
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
Console.WriteLine("एक रेंज में तत्व प्रदर्शित करें... (इंडेक्स 3 से 5 तत्व)");
demoEnum = arrList.GetEnumerator(3, 5);
while (demoEnum.MoveNext())
{
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
}
}
आउटपुट
एक रेंज में तत्व प्रदर्शित करें... (इंडेक्स 1 से 3 तत्व) Katie Taylor Steve एक रेंज में तत्व प्रदर्शित करें... (इंडेक्स 3 से 5 तत्व) Steve Nathan Carl Mark Amy
मुख्य बिंदु:
GetEnumerator(int index, int count)निर्दिष्ट रेंज के लिए एन्यूमरेटर लौटाता हैindex- प्रारंभिक इंडेक्स (शून्य-आधारित)count- पुनरावृति करने वाले तत्वों की संख्या- रेंज सरणी की सीमाओं के भीतर होनी चाहिए, अन्यथा अपवाद फेंका जाता है
- यह पूरी सूची की कॉपी बनाए बिना कुशल रेंज पुनरावृति प्रदान करता है
- आधुनिक C# में
List<T>के साथ LINQ (Skip(),Take()) का उपयोग करें