|
-
Nov 1st, 2006, 08:43 PM
#1
Thread Starter
PowerPoster
IEnumerator
Sorry for all the questions.
I know that collection implement an interface called IEnumerator which returns an IEnumerable object type? Basically, its what is used to store collections and retrieve objects from a collection. It's used in foreach loops and to use them, the class in question must implement the IEnumerator/IEnumerable interface.
I Understand the IEnumerable has 3 methods implemented into your class, Reset, MoveNext and Current?
Now, Everytime I try to read the docs or some source, I get confused as what to actually implement and how it works.
Can someone tell me how to implement the interface into a class so I can enumerate through the collection? What are the guidelines I should follow in order for me to understand/remember what to implement and how the class should look like/act?
So we can have an object of type Person. Person can be people, people would be a collection which holds persons. Using this example, how can I implement and use the IEnumerable interface on the collection, People?
-
Nov 1st, 2006, 08:58 PM
#2
Re: IEnumerator
IEnumerable is an interface you can implement on any class with collection semantics. It has one method GetEnumerator() where you return an object implementing IEnumerator which relates to your class.
When you use foreach() it expands to a call to (IEnumerable)obj->getEnumerator() and then uses the (IEnumerator) MoveNext() and Current() methods on each loop iteration.
There's an example of exactly that on the doc page for IEnumerable.
What are you stuck on?
Last edited by penagate; Nov 1st, 2006 at 09:01 PM.
-
Nov 1st, 2006, 09:01 PM
#3
Thread Starter
PowerPoster
Re: IEnumerator
that is what I read 
Im just stuck on understanding and implementing it. Just need an almost line by line explanation if possible. My mind is not working straight these past few days! 
happy bday to me...and a happy IEnumerable day...hopefully...
-
Nov 1st, 2006, 09:14 PM
#4
Re: IEnumerator
I would have thought that the method names would speak for themselves. The PeopleEnum class maintains an integer variable that represents the index of the current item. Reset sets that variable to -1 so that the enumerator is positioned before the first item. MoveNext increments that variable so the next item in the collection becomes the current item. Current gets the actual item at the current index value.
-
Nov 1st, 2006, 09:50 PM
#5
Re: IEnumerator
Happy birthday.
OK. IEnumerable just specifies that a class can be enumerated. Simple enough.
An enumerator is an object that iterates over the child items of an object with collection semantics. It specifies three methods, MoveNext(), Current(), and Reset(). You call these methods when enumerating the collection object.
IEnumerable specifies one method GetEnumerator() which must return the enumerator object for the collection class.
So, we have a person, and we have people:
Code:
class Person
{
private string _name;
public Person(string name)
{
this._name = name;
}
void say_name(TextWriter o_stream)
{
o_stream.WriteLine("Hello! My Name is " + _name);
}
}
class People
{
private Person[] _people;
public int Count;
public People(Person[] people)
{
this._people = people;
}
}
To enumerate People we must implement IEnumerable:
Code:
class People : IEnumerable
{
// [...]
public IEnumerator GetEnumerator()
{
return new PeopleEnumerator(_people);
}
}
The people enumerator implements IEnumerator to iterate through the array. An enumerator maintains an internal position within the array.
Code:
class PeopleEnumerator : IEnumerator
{
private Person[] _people;
private int _position = -1;
public PeopleEnumerator(Person[] _peopleArray)
{
this._people = _peopleArray;
}
public bool MoveNext()
{
return (++_position < _people.Length);
}
public object Current()
{
return _people[_position];
}
public void Reset()
{
_position = -1;
}
}
So, we can enumerate it manually:
Code:
People employees = new People({new Person("Fred"), new Person("Jane")});
IEnumerable collection = (IEnumerable)employees;
IEnumerator enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
{
((Person)enumerator.Current()).say_name(Console.Out);
}
enumerator.Reset();
Or using foreach():
Code:
foreach(Person p in employees)
{
p.say_name(Console.Out);
}
If you are using .NET 2.0 (which you haven't specified, naughty) you can use IEnumerator<T> and the yield keyword as shown here:
http://www.ondotnet.com/pub/a/dotnet...-04/index.html
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|