产量在VB.NET [英] Yield in VB.NET

查看:223
本文介绍了产量在VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#有一个名为产量的关键字。 VB.NET没有这个关键字。怎么有Visual Basic程序员得到解决缺乏这一关键字的?难道他们实现他们自己的迭代器类?或者,他们试图和code,以避免迭代器的需要?

C# has the keyword called yield. VB.NET lacks this keyword. How have the Visual Basic programmers gotten around the lack of this keyword? Do they implement they own iterator class? Or do they try and code to avoid the need of an iterator?

借助产生关键字不强制编译器做一些编码幕后。 在C#中的迭代器及其后果的实施(第1部分) 的有一个很好的例子。

The yield keyword does force the compiler to do some coding behind the scenes. The implementation of iterators in C# and its consequences (part 1) has a good example of that.

推荐答案

C#转换在编译时产量关键字进入状态机。 VB.NET没有屈服的关键字,但它确实有它自己的机制,安全地嵌入的功能是不容易获得在C#中的状态。

Note: This answer is old now. Iterator blocks have since 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 does have its own mechanism for safely embedding state within a function that is not easily available in C#.

在C#静态关键字通常翻译为Visual Basic中使用共享关键字,但有两个地方事情变得扑朔迷离。一个是一个C#静态类是确实在Visual Basic中,而不是共享类模块(你会觉得他们会让你code这两种方式在Visual Basic中,但NOOOO)。另一种是,VB.NET确实有其自身的静态关键字。然而,静态有不同的含义在VB.NET。

The C# static keyword is normally translated to Visual Basic using the Shared 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 Static keyword. However, Static has a different meaning in VB.NET.

您使用VB.NET的静态关键字来声明一个变量的函数里面,当你这样做的变量保留在函数调用它的状态。这不仅仅是宣布在C#中的私有静态类成员不同,因为在VB.NET静态函数成员保证同样是线程安全的,因为编译器将其使用Monitor类在编译时。

You use the Static 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.

那么,为什么在这里写这篇文章呢?那么,它应该有可能建立一个可重复使用的通用迭代器< T> 类(或迭代器(对T)在VB.NET)。在这个类中,你将实现状态机使用C#,用收益率()中断()对应方法到C#关键字。然后,你可以使用一个静态实例(在VB.NET意义上)的功能,使之能最终做到pretty的大致相同的工作作为C#的收益率约的code(丢弃类实行本身,因为它会被无限重复使用)相同的量。

So why write all this here? Well, it should be possible to build a re-usable generic Iterator<T> class (or Iterator(Of T) in VB.NET). In this class you would implement the state machine used by C#, with Yield() and Break() 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 yield in about the same amount of code (discarding the class implemenation itself, since it would be infinitely re-usable).

我没有关心不够有关产量尝试它自己,但它的的是可行的。这就是说,它也不会妄自菲薄,因为C#团队成员埃里克利珀称之为<一个href="http://blogs.msdn.com/ericlippert/archive/2009/08/24/iterator-blocks-part-seven-why-no-anonymous-iterators.aspx"相对=nofollow>最复杂的转型,编译器。我也开始相信,因为我写的这个初稿在一年前,它是不是真的有可能以一种有意义的方式,直到Visual Studio 2010中出来,因为这需要发送多个lambda表达式的迭代器类等,真正做到实际我们需要[.NET 4 3 的多行lambda表达式的支持。

I haven't cared enough about Yield to attempt it myself, but it should be doable. That said, it's also far from trivial, as C# team member Eric Lippert calls this "the most complicated transformation in the compiler." 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 [.NET 43's support for multi-line lambdas.

这篇关于产量在VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆