When reading a post some points were given without example :
To implement IEnumerable / IEnumerable, you must provide an enumerator :
•If the class is "wrapping" another collection, by returning the wrapped collection's enumerator.
•Via an iterator using yield return.
•By instantiating your own IEnumerator/IEnumerator implementation
(My baby mind interprets it as)
(Point 1)
If the class is "wrapping" another collection, by returning the
wrapped collection's enumerator.
Will it mean ..
class StringCollections
{
//A class is wrapping another collection
string[] names=new string {“Jon Skeet”,”Hamish Smith”,
”Marc Gravell”,”Jrista”,”Joren”};
//by returning the wrapped collection’s enumerator
public IEnumerator GetEnumerator( )
{
// What should I return here ?
//like the following ?
yield return names[0];
yield return names[1];
yield return names[2];
....
(or)
foreach(string str in names)
{
yield return str;
}
}
}
(Point 2)
•Via an iterator using yield return.(This point was well explained
by Marc Gravell)
Point 3
By instantiating your own IEnumerator/IEnumerator<T> implementation*
What does point 3 represent here?,since no example,i did not get that one. Does it mean,i can build custom enumerator ..(right?).My question here is when prebuild enumerator/enumerators are sufficient (as a beginner i should not blindly confirm this) for iterations why should i look after the custom one ?Nice example will clarify my doubt.
Thanks for reading this long winded story and kind response.