IEnumerable<T>.GetEnumerator( ) return value


S

Steve Richter

very confused on how to implement the IEnumerable and IEnumerator
interfaces on a generic type. I understand I should just use a
foreach loop in the GetEnumerator method and use "yield return",
etc.

would like to know how implement my generic GetEnumerator to yield
return an interface to IEnumerator<T>. I am getting a compile error
that reminds me a lot of C++ template programming.

thanks,



public class LoadedData : IEnumerable<string>, IEnumerator<string>
{
LinkedList<string> mLoadedLines = null;

public LoadedData()
{
mLoadedLines = new LinkedList<string>( );
mLoadedLines.AddLast( "abc" );
mLoadedLines.AddLast( "efg" );
}

IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
yield return (IEnumerator<string>)this; // ??? this does not
compile
// foreach (string line in mLoadedLines)
// {
// yield return line;
// }
}
 
Ad

Advertisements

R

rossum

very confused on how to implement the IEnumerable and IEnumerator
interfaces on a generic type. I understand I should just use a
foreach loop in the GetEnumerator method and use "yield return",
etc.

would like to know how implement my generic GetEnumerator to yield
return an interface to IEnumerator<T>. I am getting a compile error
that reminds me a lot of C++ template programming.

thanks,



public class LoadedData : IEnumerable<string>, IEnumerator<string>
{
LinkedList<string> mLoadedLines = null;

public LoadedData()
{
mLoadedLines = new LinkedList<string>( );
mLoadedLines.AddLast( "abc" );
mLoadedLines.AddLast( "efg" );
}

IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
yield return (IEnumerator<string>)this; // ??? this does not
compile
// foreach (string line in mLoadedLines)
// {
// yield return line;
// }
}
You need to explicitly write both the generic and the non-generic
versions:

class TestIEnum : IEnumerable<string> {

string[] m_strings;

// Generic version
public IEnumerator<string> GetEnumerator() {
foreach (string s in m_strings) {
yield return s;
}
}

// Non-generic version - calls generic version
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}

rossum
 
Ad

Advertisements

S

Steve Richter

very confused on how to implement the IEnumerable and IEnumerator
interfaces on a generic type. I understand I should just use a
foreach loop in the GetEnumerator method and use "yield return",
etc.
would like to know how implement my generic GetEnumerator to yield
return an interface to IEnumerator<T>. I am getting a compile error
that reminds me a lot of C++ template programming.

public class LoadedData : IEnumerable<string>, IEnumerator<string>
{
LinkedList<string> mLoadedLines = null;
public LoadedData()
{
mLoadedLines = new LinkedList<string>( );
mLoadedLines.AddLast( "abc" );
mLoadedLines.AddLast( "efg" );
}
IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
yield return (IEnumerator<string>)this; // ??? this does not
compile
// foreach (string line in mLoadedLines)
// {
// yield return line;
// }
}

You need to explicitly write both the generic and the non-generic
versions:

class TestIEnum : IEnumerable<string> {

string[] m_strings;

// Generic version
public IEnumerator<string> GetEnumerator() {
foreach (string s in m_strings) {
yield return s;
}
}

// Non-generic version - calls generic version
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}

rossum- Hide quoted text -

- Show quoted text -

thanks. As I am understanding it, yield return is a way to resume
execution at the stmt after the yield return on the next call to the
iterator. pretty slick.

if I want to maintain state, for example in order to make the iterator
threadsafe, my iterator method should accept parameters and return
IEnumerable.

the C# cookbook by O'Reily has some good examples.

-Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top