The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,856 other followers

.NET/C#: some starting posts on the `yield` keyword.

Posted by jpluimers on 2013/08/08

Peter Leslie Morris asked if Delphi already incorporates the `yield` keyword that C# had introduced in C# 2.

Delphi doesn’t, but for the people interested what it does in C#:

Basically `yield` is syntactic sugar to make it a lot easier to write methods that return enumerators of some sort.

It delays (hence the yield keyword) execution until the enumerator as actually being used.

It is one of the hardest C# things to master (it is the most complicated transformation in the compiler, followed by anonymous methods – well maybe with the exception of async/await), but it can be very useful.

VB.NET doesn’t have it either (thanks André!) has it too, but and also has iterator blocks.

Some start posts on yield:


10 Responses to “.NET/C#: some starting posts on the `yield` keyword.”

  1. […] year’s summer, I posted some .NET/C#: some starting posts on the yield keyword and mentioned that async and await might be the most complicated compiler […]

  2. VB.NET has – similar to C# – the Yield keyword…

    Sub Main()
    For Each number As Integer In SomeNumbers()
    Console.Write(number & ” “)
    ‘ Output: 3 5 8
    End Sub

    Private Iterator Function SomeNumbers() As System.Collections.IEnumerable
    Yield 3
    Yield 5
    Yield 8
    End Function

  3. Delphi has yield return ;) See

    It is implemented for win32 and win64 using the fiber winapi.

    • jpluimers said

      Wow, that is COOL!
      Any chance of making that X-platform?

      • Only if someone points me to (or develops) a coroutine implementation for the other platforms.

        Otherwise there is still the possibility to use a thread based implementation (which is of course much slower due to the context switches)

        • jpluimers said

          Maybe ask about that on StackOverflow? Despite many moderators there thinking they are demi-Gods, it is still the best Q&A site for programmers.

      • I have a thread based implementation but at least on windows it suffers from the context switching (talking about several thousands of iterations though) whereas the fiber based implementation is almost instant.
        And I haven’t tested it on other platforms than windows yet. But as it simply follows the producer/consumer pattern it should work.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: