Jiang (6) [Avatar] Offline
#1
The transition from introducing IEnumerator to IEnumerable is too jumpy without much explanation:

the bottom of page 165 of

public IEnumerator GetEnumerator()
{
for (int index=0; index < values.Length; index++)
{
yield return values[(index+startingPoint)%values.Length];
}
}

to page 167
static readonly string Padding = new string(' ', 30);
static IEnumerable<int> GetEnumerable()

It didn't tell me why you go from using yield return from inside IEnumerator to inside IEnumerable. What's the connection?

I googled and found the explanation from Chris Nahr (http://forums.msdn.microsoft.com/en-US/csharplanguage/thread/4dbe17cb-9da3-4f57-8a10-01e04f8a239b/)

The hidden state machine that the compiler builds when it sees a yield statement is always an implementation of IEnumerator, and it's exactly the same code whether you specify IEnumerator or IEnumerable.

When you specify IEnumerable you get the same state machine with the same IEnumerator type, but wrapped in an outer IEnumerable type with an extra GetEnumerator method. Since the IEnumerator implementation is the simpler and more fundamental case it makes sense that it would be exposed alongside the IEnumerable case.

When would you want to use IEnumerator with yield? Quite simply, whenever you want to design your own outer class that implements IEnumerable. In the generic case you'd implement "public IEnumerator<T> GetEnumerator()" within that class-- this fulfils IEnumerable<T> and makes foreach work -- and the separate explicit non-generic overload for IEnumerable without <T>:

public class MyFunnyCollectionType: IEnumerable<T> {

...lots of usual methods & properties here...

public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < 10; i++)
yield return i;
}

IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}

I wish Jon hadn't been so hasty in treating this difficult topic.
jon.skeet (448) [Avatar] Offline
#2
Re: IEnumerator, IEnumerable, confusing moment in reading the book
I'll add a note to the web site when I get time. I think it's relatively unimportant to most people, and on P166 I do state that you can use iterator blocks for both IEnumerator and IEnumerable (and their generic equivalents) but I didn't go into more detail than that.

Hopefully the note will suffice for the moment - it's unlikely that this would be changed on a second printing, but if there's ever a new edition (e.g. when C# 4 comes out) I'll bear this in mind as a topic to expand a bit.

Jon
Jiang (6) [Avatar] Offline
#3
Re: IEnumerator, IEnumerable, confusing moment in reading the book
Thanks Jon for the response.

I liked your book a lot because of the way it's written and a lot of effort put into making the transition from C#1 to C#3 a must instead of a choice. I am from ASP.NET background and usually don't get deep into C# specifics. I forced myself to read CLR via C#. it's a great book but C# in depth is a step further in making people want to use C# 3 features and follow the revolution of functional programming introduced by C# 3.


I think the greatest success of your book is that you motivated a lot of people to start the C# 3 journey with great enthusiasm.
jon.skeet (448) [Avatar] Offline
#4
Re: IEnumerator, IEnumerable, confusing moment in reading the book
> I liked your book a lot because of the way it's
> written and a lot of effort put into making the
> transition from C#1 to C#3 a must instead of a
> choice. I am from ASP.NET background and usually
> don't get deep into C# specifics. I forced myself to
> read CLR via C#. it's a great book but C# in depth
> is a step further in making people want to use C# 3
> features and follow the revolution of functional
> programming introduced by C# 3.

Thank you. I'm ashamed to say I haven't read "CLR via C#" yet - but it's on my list.

> I think the greatest success of your book is that you
> motivated a lot of people to start the C# 3 journey
> with great enthusiasm.

That's extremely kind of you. Furthermore I should apologise for the slightly testy tone of my previous response. I think it's fabulous that readers like yourself want to know more details of what's going on. That's exactly the kind of readership I want - and I'm sorry that on this particular occasion the book didn't go into enough detail. Hopefully I can make up for it with the extra note - I'll do a proper job on it, I promise smilie

Jon
Abacus (59) [Avatar] Offline
#5
Re: IEnumerator, IEnumerable, confusing moment in reading the book
> I think it's relatively unimportant to most people ...

I have no idea how the majority of readers of technical books feel about such things, but I personally find it very helpful to be given more rather than less detail. Because very few books do provide all the detail I want, I end up shelling out far too many dollars to buy piles of books that take up precious shelf space, and then trying to put together scraps obtained from each, much like piecing together a jigsaw puzzle. I suppose some might say that that is a useful intellectual exercise, but I would much prefer to have a single book in my hands that explained everything in sufficient detail to eliminate all confusion. I wonder whether any publishers have done any scientific studies on reader attitudes to find out what the majority really wants.

Anyway, thanks to Jiang for raising this issue. I have found more than one book confusing on the topic of "yield return" statements and the differences between IEnumerable and IEnumerator. On this topic, Jon's book is the best I have read so far.
mgravell (64) [Avatar] Offline
#6
Re: IEnumerator, IEnumerable, confusing moment in reading the book
Re the good write-up of iterator block - I agree (as I do with the wider point of detail). I see confused questions about iterator blocks very regularly on the managed MS forums (oddly not so much on usenet), and I keep referring people to the sample chapter (6?). I hope that at least some of them decide to buy based on this chapter (I generally suggest it...), as it gives a good "feel" for what the rest of the book is like.

Marc
Jiang (6) [Avatar] Offline
#7
Re: IEnumerator, IEnumerable, confusing moment in reading the book
I remembered trying to read Quantum Chemistry and tried very hard to understand what it says without much success. I don't believe that this topic is beyond comprehension, but the learning curve is just too steep for me and trying to make the slope less steep requires years of preparation. So I just gave up on Quantum Chemistry feeling defeated. Later on, I gave up on Chemistry all together and never looked back.

Thinking back on this, my exasperation with Chemistry mostly stems from lack of good books that makes it easy to understand and interesting.

C# in depth greatly elevates my interest in moving from C# 1.0 through C# 2.0 and onto C# 3.0 and making the transition by just reading one book! I actually read "Linq in Action" and can follow along with what the book says. But it didn't really pique my interest in really getting my hands dirty on C# 3.0 features.

Another thing I found extremely useful is Snippy and Linqpad. Without those tools, learning C# 3.0 is much harder. Now, I can't go back without using Lambda expression.
jon.skeet (448) [Avatar] Offline
#8
Re: IEnumerator, IEnumerable, confusing moment in reading the book
Okay, well I said I'd write up the implementation in more detail... little did I know how long it would be! (It's about 70% of the length of chapter 6, in terms of words - although there's a lot of example code.)

It's now available at
http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx

Comments very welcome, whether here or to my mailbox (skeet@pobox.com)

Thanks for suggesting it, Jiang! Hopefully others will benefit too.

Jon
MickeyP (20) [Avatar] Offline
#9
Re: IEnumerator, IEnumerable, confusing moment in reading the book
On the point of more detail vs less, this is all a matter of personal preference, but mine is for something thats a bit shorter and to the point, the benefit being you can read it quickly, rather than it becoming another door stopping tome.

So generally would prefer less detail than more. Obviously enough to be able to do most things and can look up in reference books for the really esoteric stuff. Thats where the skill of the author comes in in separating the wheat from chaff.

I think theres a need for Reference Books that contain all the stuff and separate teaching books that contain the 65% of the stuff that you'll actually come across in everyday in real life.