what is the use of the GetEnumerator (). For example if I write a small code as
Code:
using System; using System.Collections;
class MyClass : IEnumerator, IEnumerable {
int[] intst = { 1,2,3,4 };
int idx = -1;
public IEnumerator GetEnumerator() {
return this; //-------------------------------------------------------------->>>>>>>>>>>
}
// to implement IEnumerator.
public object Current {
get {
return intst[idx]; //--------------------------------------------------->>>>>>>>>>>>>>>
}
}
// Advance to the next object.
public bool MoveNext() {
if(idx == intst.Length-1) {
Reset();
return false;
}
idx++;
return true;
}
public void Reset() { idx = -1; }
}
class Enumer {
static void Main() {
MyClass mc = new MyClass();
// To display the contents of mc.
foreach(int i1 in mc)
Console.Write(i1 + " ");
Console.WriteLine();
}
}
Here is the output:
1 2 3 4
Here see the lines with multiple arrowheads. T he second one I can able to follow that it returns the current object
at idx ------- What i do not follow is the FIRST arrow headed code line
"
public IEnumerator GetEnumerator() {
return this;
"
---------- what is the use of it i mean?