what is the use of the GetEnumerator (). For example if I write a small code as
=================================
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;
"
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
The foreach statement requires that either IEnumerable be implemented or that GetEnumerator is declared. The key aspect of IEnumerable is that it requires GetEnumerator to be implemented. Somehow foreach must be able to call GetEnumerator(). The IEnumerator
that is returned is then used internally by the foreach mechanism.
So, what happens is that when the foreach is executed the runtime must be able to find GetEnumerator for the variable that is being iterated (mc in your case).
What about the 'weird' body of GetEnumerator() - return this; ? GetEnumerator must return something which implements the IEnumerator interface. In your case the class MyClass implements IEnumerator and so all that GetEnumerator needs to do is return the
current object ('this'). It is possible to have a property within your class which implements IEnumerator and provides the Current, MoveNext and Reset implementations. In that case you would return the value of that property.
Note that arrays implement IEnumerable so your sample code could be simplified to
class MyClass : IEnumerable {
int[] intst = {1,2,3,4};
public IEnumerator GetEnumerator() { return inst.GetEnumerator();}
}
amigo 1
Member
60 Points
199 Posts
GetEnumerator ()what is the use
Nov 08, 2012 04:34 AM|LINK
what is the use of the GetEnumerator (). For example if I write a small code as
=================================
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;
"
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---------- what is the use of it i mean?
Paul Linton
Star
13403 Points
2531 Posts
Re: GetEnumerator ()what is the use
Nov 08, 2012 06:30 AM|LINK
The foreach statement requires that either IEnumerable be implemented or that GetEnumerator is declared. The key aspect of IEnumerable is that it requires GetEnumerator to be implemented. Somehow foreach must be able to call GetEnumerator(). The IEnumerator that is returned is then used internally by the foreach mechanism.
So, what happens is that when the foreach is executed the runtime must be able to find GetEnumerator for the variable that is being iterated (mc in your case).
What about the 'weird' body of GetEnumerator() - return this; ? GetEnumerator must return something which implements the IEnumerator interface. In your case the class MyClass implements IEnumerator and so all that GetEnumerator needs to do is return the current object ('this'). It is possible to have a property within your class which implements IEnumerator and provides the Current, MoveNext and Reset implementations. In that case you would return the value of that property.
Note that arrays implement IEnumerable so your sample code could be simplified to
class MyClass : IEnumerable { int[] intst = {1,2,3,4}; public IEnumerator GetEnumerator() { return inst.GetEnumerator();} }Your Main method remains exactly the same.
amigo 1
Member
60 Points
199 Posts
Re: GetEnumerator ()what is the use
Nov 09, 2012 11:35 AM|LINK
Thank You teacher