473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

IEnumerable<T>.GetEnumerator( ) return value

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<stringmLoadedLines = null;

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

IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
yield return (IEnumerator<string>)this; // ??? this does not
compile
// foreach (string line in mLoadedLines)
// {
// yield return line;
// }
}

Feb 20 '07 #1
2 19842
On 20 Feb 2007 11:11:37 -0800, "Steve Richter"
<St************@gmail.comwrote:
>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<stringmLoadedLines = null;

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

IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
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<stringGetEnumerator() {
foreach (string s in m_strings) {
yield return s;
}
}

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

rossum

Feb 20 '07 #2
On Feb 20, 5:17 pm, rossum <rossu...@coldmail.comwrote:
On 20 Feb 2007 11:11:37 -0800, "Steve Richter"

<StephenRich...@gmail.comwrote:
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<stringmLoadedLines = null;
public LoadedData()
{
mLoadedLines = new LinkedList<string>( );
mLoadedLines.AddLast( "abc" );
mLoadedLines.AddLast( "efg" );
}
IEnumerator<stringIEnumerable<string>.GetEnumerato r()
{
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<stringGetEnumerator() {
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
Feb 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
3
by: Adam Clauss | last post by:
I am developing an abstract class which implements IEnumerable<T>. I need the actual implemented methods here to be abstract as well - they will be implemented by MY subclasses. However, I...
1
by: nick_tucker | last post by:
Hi, Could someone tell me which of the following is correct if I want to make the MyCollection class below enumerable public class MyCollection:System.Collections.IEnumerable { private...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
5
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
2
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... ...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
1
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
Something I found surprising this week involves the IEnumerable<Tinterface. I have a class that I wrote a couple of years ago which implements the IEnumerable interface. This week I realized it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.