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;
// }
}
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;
// }
}