Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen is custom enumerable/collection useful?
    primarykey
    data
    text
    <p>I am dropping this line after having visited different websites to try understand real time example of using custom enumeration. I got examples. But they lead me to confusion.</p> <p><strong>Example</strong> </p> <p><strong>Take 1</strong></p> <pre><code>class NumberArray { public int[] scores; public NumberArray() { } public NumberArray(int[] scores) { this.scores = scores; } public int[] Scores { get {return scores;} } } </code></pre> <p><strong>Take 2</strong></p> <pre><code>public class Enumerator : IEnumerator { int[] scores; int cur; public Enumerator(int[] scores) { this.scores = scores; cur = -1; } public Object Current { get { return scores[cur]; } } public void Reset() { cur = -1; } public bool MoveNext() { cur++; if (cur &lt; scores.Length) return true; else return false; } } public class Enumerable : IEnumerable { int[] numbers; public void GetNumbersForEnumeration(int[] values) { numbers = values; for (int i = 0; i &lt; values.Length; i++) numbers[i] = values[i]; } public IEnumerator GetEnumerator() { return new Enumerator(numbers); } } </code></pre> <p><strong>Main</strong></p> <pre><code>static void Main() { int[] arr = new int[] { 1, 2, 3, 4, 5 }; NumberArray num = new NumberArray(arr); foreach(int val in num.Scores) { Console.WriteLine(val); } Enumerable en = new Enumerable(); en.GetNumbersForEnumeration(arr); foreach (int i in en) { Console.WriteLine(i); } Console.ReadKey(true); } </code></pre> <p>In take 2, I followed the custom iteration to iterate the same integer array as I did in take 1. Why should I <strong>beat about the bush to iterate an integer by using custom iteration</strong>?</p> <p>Probably I missed out the <strong>real-time custom iteration</strong> need. Can you explain me the task which I can't do with existing iteration facility? (<strong>I just finished my schooling, so give me a simple example, so that I can understand it properly</strong>).</p> <p><strong>Update :</strong> <strong>I took those examples from some site. There is nothing special in that code, we can achieve it very simply even without using custom iteration, my interest was to know the real scenario where custom iteration is quite handy.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload