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.
Example
Take 1
class NumberArray
{
public int[] scores;
public NumberArray()
{
}
public NumberArray(int[] scores)
{
this.scores = scores;
}
public int[] Scores
{
get {return scores;}
}
}
Take 2
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 < 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 < values.Length; i++)
numbers[i] = values[i];
}
public IEnumerator GetEnumerator()
{
return new Enumerator(numbers);
}
}
Main
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);
}
In take 2 ,I folowed the custom iteration to iterate the same integer array as i did in take 2.Why should i beat about the bush to iterate an integer by using custom iteration?
Probably i missed out the real-time custom iteration need.Can you explain me the task which i can't do with exisiting iteration facility.(Just I finished my schooling,so give me a simple example ,so that i can understand it properly).
Update : Those examples i took from some site.As nothing special in that code ,moreover we can achieve it very simply even without using custom iteration,my interest was to know the real scenario where cutom iteration is quite handy.