473,321 Members | 1,669 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,321 software developers and data experts.

yield block

"Any parameters passed to the method containing the yield block are
added as public fields to the generated enumerator class"

Why does compiler adds these parameters as public fields?
please explain the need for that.

Thanks
Oct 15 '08 #1
5 1372
Think how the iterator block must actually work... it isn't a
regular .NET method - the compiler actually strips it down into
pieces, and each time MoveNext() is called it runs the next segment of
your code. In the time between MoveNext() calls, it needs to store all
the data somewhere (they can't live on the stack if the method has
exited), so it uses fields.

You might want to look at the (free) chapter 6 from Jon Skeet's C# in
Depth, which explains all.

Marc
Oct 15 '08 #2
Marc Gravell wrote:
Think how the iterator block must actually work... it isn't a
regular .NET method - the compiler actually strips it down into
pieces, and each time MoveNext() is called it runs the next segment of
your code. In the time between MoveNext() calls, it needs to store all
the data somewhere (they can't live on the stack if the method has
exited), so it uses fields.
I suspect the question is why these fields would be public.
>
You might want to look at the (free) chapter 6 from Jon Skeet's C# in
Depth, which explains all.

Marc

Oct 15 '08 #4
On Oct 15, 3:14*am, puzzlecracker <ironsel2...@gmail.comwrote:
"Any parameters passed to the method containing the yield block are
added as public fields to the generated enumerator class"

Why does compiler *adds these parameters as public fields?

please explain the need for that.
It was easier to implement the compiler that way. It allowed them to
reuse some other code (from anonymous methods, I believe). Yes, it
would be a bit cleaner to pass the values into a constructor for the
generator class, but as this is all generated code which can't be
referred to at compile-time, I'm willing to give them a bit of
leeway :)

Jon
Oct 15 '08 #5
I suspect the question is why these fields would be public.

Ahh - if the emphasis is on the word "public", then I'd agree. In
fact, Jon and I had a discussion on this earlier in the week;
"private", or at least "internal" would seem more likely - but you'd
need reflection to abuse them, and with reflection (and enough trust)
all bets are off anyway...

Marc
Oct 15 '08 #6

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

Similar topics

2
by: yyhhjj | last post by:
I created a test program to implement an iterator. First, I used 'yield break' in the iterator, it worked normally. Then, I simply used 'break' in the places of 'yield break', it still worked...
2
by: IntraRELY | last post by:
I know this isnt really a VB question per say, but is what I am developing in VB.NET and Excel is the only place that has provided direction. I wanted to ask the public if perhaps you could lend me...
22
by: Mateuszk87 | last post by:
Hi. may someone explain "yield" function, please. how does it actually work and when do you use it? thanks in forward mateusz
2
by: cartoper | last post by:
I am coming to VB.Net from C#. One feature I really like about C# is the yield contextual keyword. Here is what MSDN says about it: -------------- Used in an iterator block to provide a value...
2
by: Adam Atlas | last post by:
I'm trying to emulate the Python 2.5 behaviour (PEP 342) of generator functions where the `yield` statement is in a `try/finally` block. Basically, where the `finally` block is guaranteed to run...
3
by: Ehsan | last post by:
hi coulde any one show me the usage of "yield" keyword specially in this example: """Fibonacci sequences using generators This program is part of "Dive Into Python", a free Python book for...
1
by: castironpi | last post by:
What if I say oath= yield or other= yield ?
7
by: Alex Bryan | last post by:
Okay, so i don't really understand the Yield thing and i know it is useful. I've read a few things about it but it is all programming jargon and so basically it is hard for me to understand. So can...
5
by: defn noob | last post by:
def letters(): a = xrange(ord('a'), ord('z')+1) B = xrange(ord('A'), ord('Z')+1) while True: yield chr(a) yield chr(B) Traceback (most recent call last):
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.