I learned that when the control of a thread is given back from the function currently executing to its caller, its stack frame will be destroyed in the assembly, but it seems this doesn't hold true for the C# keyword yield
.
Asked
Active
Viewed 90 times
2

Elijah Lee
- 39
- 2
-
I don't know enough to write an authoritative answer, but my understanding is that `yield` creates an anonymous class (the enumerator that is being returned) that can then hold all the necessary information to perform the deferred execution. – BradleyDotNET Oct 07 '19 at 16:35
-
1@dymanoid I think this question is more asking how `yield` is implemented, rather than it's usage – BradleyDotNET Oct 07 '19 at 16:36
-
1@BradleyDotNET, there are answers in that question which explain pretty much. – dymanoid Oct 07 '19 at 16:37
-
Since the linked answer doesn't have any SharpLab links to play with, [here's one](https://sharplab.io/#v2:CYLg1APgAgDABFAjAFgNwFgBQUDMCBMcAwnAN5ZyUJ5I4A8AlgHYAuAfHALIAUAlGRSpCkiBAHY4iDJiHDEoqBPzShAXyyqgA===) – canton7 Oct 07 '19 at 16:37
-
@dymanoid This one comes close... https://stackoverflow.com/a/44801657/1783619 but they aren't the same question, even if one of the answers sort of answers this one. We can agree to disagree though – BradleyDotNET Oct 07 '19 at 16:40
-
1@BradleyDotNET Check out this one: https://stackoverflow.com/a/39507/1086121 and this one: https://stackoverflow.com/a/39482/1086121 – canton7 Oct 07 '19 at 16:41
-
1googling led me to this : https://csharpindepth.com/articles/IteratorBlockImplementation I don't know the date of this post, I guess the core principles are still true. – LeGEC Oct 07 '19 at 16:44
-
@canton7 Those would be good answers on this question (though the 1st is nearly link-only and the second very short). Still disagree it makes this one a dupe :) – BradleyDotNET Oct 07 '19 at 16:44
-
1@LeGEC: Yup, while some of the implementation details may have changed, the core ideas haven't. – Jon Skeet Oct 07 '19 at 16:48