Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Note: This answer is old now. Iterator blocks have since been added to <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" rel="nofollow noreferrer">VB.NET</a></h2> <p>C# translates the yield keyword into a state machine at compile time. VB.NET does not have the yield keyword, but it does have its own mechanism for safely embedding state within a function that is not easily available in C#.</p> <p>The C# <code>static</code> keyword is normally translated to Visual Basic using the <code>Shared</code> keyword, but there are two places where things get confusing. One is that a C# static class is really a Module in Visual Basic rather than a Shared class (you'd think they'd let you code it either way in Visual Basic, but noooo). The other is that VB.NET does have its own <code>Static</code> keyword. However, <code>Static</code> has a different meaning in VB.NET.</p> <p>You use the <code>Static</code> keyword in VB.NET to declare a variable inside a function, and when you do the variable retains its state across function calls. This is different than just declaring a private static class member in C#, because a static function member in VB.NET is guaranteed to also be thread-safe, in that the compiler translates it to use the Monitor class at compile time.</p> <p>So why write all this here? Well, it should be possible to build a re-usable generic <code>Iterator&lt;T&gt;</code> class (or <code>Iterator(Of T)</code> in VB.NET). In this class you would implement the state machine used by C#, with <code>Yield()</code> and <code>Break()</code> methods that correspond to the C# keywords. Then you could use a static instance (in the VB.NET sense) in a function so that it can ultimately do pretty much the same job as C#'s <code>yield</code> in about the same amount of code (discarding the class implemenation itself, since it would be infinitely re-usable).</p> <p>I haven't cared enough about Yield to attempt it myself, but it <em>should</em> be doable. That said, it's also far from trivial, as C# team member Eric Lippert calls this "<a href="http://blogs.msdn.com/ericlippert/archive/2009/08/24/iterator-blocks-part-seven-why-no-anonymous-iterators.aspx" rel="nofollow noreferrer">the most complicated transformation in the compiler</a>." </p> <p>I have also come to believe since I wrote the first draft of this over a year ago that it's not really possible in a meaningful way until Visual Studio 2010 comes out, as it would require sending multiple lambdas to the Iterator class and so to be really practical we need <a href="http://en.wikipedia.org/wiki/.NET_Framework" rel="nofollow noreferrer">.NET 4</a>'s support for multi-line lambdas.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload