468,545 Members | 1,710 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

Really, how do you make enumerations?

I had VS (2008) telling me my class isn't enumerable when trying to use a foreach loop. This happens quite often, and every time I Google around for an hour trying to find a solution that applies to my case. It needs to be understandable and easy to implement. It needs to work with List<Tcollections. It should be the best practice in .NET 3.5. Can anyone point me once and for all to the right solution? The best I can find right now is this:

http://msdn.microsoft.com/en-us/libr...numerable.aspx

But I don't want to write all that every time I make a collection lass, and I don't like the public class variable in the enumerator class. Didn't enumerations get a lot easier in .NET 2.0?

Gustaf
Sep 24 '08 #1
2 1000
On Sep 24, 11:40*am, Gustaf <gust...@algonet.sewrote:

<snip>
But I don't want to write all that every time I make a collection lass,
and I don't like the public class variable in the enumerator class.
Didn't enumerations get a lot easier in .NET 2.0?
Implementing IEnumerable and IEnumerator (and the generic equivalents)
became a lot easier in C# 2 with iterator blocks (method
implementations using "yield return" and "yield break" statements).

See the following links for a couple of articles and a free chapter
from my book (C# in Depth) which covers iterator blocks:
http://csharpindepth.com/Articles/Ch...Iterators.aspx
http://csharpindepth.com/Articles/Ch...mentation.aspx
http://www.manning-source.com/books/...ter6sample.pdf

Jon

Sep 24 '08 #2
t needs to work with List<Tcollections.
Can you clarify what you have already? If you have a class that is
encapsulating a single list, then you can simply forward the list's
enumerator:

class Test : IEnumerable<string>
{
private List<stringinnerList = new List<string>();

public IEnumerator<stringGetEnumerator()
{
return innerList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

If you have multiple lists, you can use an iterator block to flatten
them:

class Test : IEnumerable<string>
{
private List<stringlistA = new List<string>(),
listB = new List<string>();

public IEnumerator<stringGetEnumerator()
{
foreach (string s in listA) {yield return s;}
foreach (string s in listB) { yield return s; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

If that doesn't help, can you clarify what you have?

Marc
Sep 24 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Christopher Benson-Manica | last post: by
3 posts views Thread by JoeH | last post: by
5 posts views Thread by Seamus M | last post: by
2 posts views Thread by Glenn | last post: by
1 post views Thread by Oleg Ogurok | last post: by
4 posts views Thread by ChrisB | last post: by
77 posts views Thread by Ben Finney | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.