Note. This answer is old. Since then, Iterator blocks have been added to VB.NET
C # translates the yield keyword into a state machine at compile time. VB.NET does not have the yield keyword, but it has its own mechanism for safely embedding state in a function that is not easily accessible in C #.
The C # static usually translated into Visual Basic using the Shared keyword, but there are two places where things get confused. One of them is that the C # static class is really a module in Visual Basic, and not for the Shared class (you think that they will let you code it anyway in Visual Basic, but noooo). Another is that VB.NET has its own static . However, static has a different meaning in VB.NET.
You use the static in VB.NET to declare a variable inside a function, and when you execute a variable, it retains its state when calling functions. This is different from simply declaring a private static class member in C #, because a static function member in VB.NET is also guaranteed to be thread safe, as the compiler translates it to use the Monitor class at compile time.
So why write all this here? Well, it should be possible to create a reusable generic class Iterator<T> (or Iterator(Of T) in VB.NET). In this class, you would use the state machine used by C #, with the Yield() and Break() methods that match the C # keywords. Then you can use a static instance (in the sense of VB.NET) in a function so that it can ultimately do almost the same job as C # yield , in about the same amount of code (discarding the class implementation itself, since it will be infinite reused).
I did not care about Revenue to try to do it myself, but it should be doable. However, this is also far from trivial, as C # team member Eric Lippert calls this โthe most complicated compiler conversion โ. I also believed since I wrote the first draft of this year ago that it really is not possible in a meaningful way until Visual Studio 2010 appears, as it will require sending several lambdas to the Iterator class and to be really we need [.NET 4 3 support for multi-line lambdas.
Joel Coehoorn Dec 19 '08 at 21:00 2008-12-19 21:00
source share