VB.NET 中的产量 [英] Yield in VB.NET

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

问题描述

C# 具有名为 yield 的关键字.VB.NET 缺少这个关键字.Visual Basic 程序员如何解决缺少此关键字的问题?他们是否实现了他们自己的迭代器类?或者他们是否尝试编写代码以避免需要迭代器?

yield 关键字确实会强制编译器在幕后做一些编码.C# 中迭代​​器的实现及其后果(第 1 部分) 有一个很好的例子.

解决方案

注意:这个答案现在已经过时了.迭代器块已被添加到 VB.NET

C# 在编译时将 yield 关键字转换为状态机.VB.NET 没有 yield 关键字,但它有自己的机制来安全地将状态嵌入在 C# 中不容易获得的函数中.

C# static 关键字通常使用 Shared 关键字转换为 Visual Basic,但有两个地方会让人感到困惑.一个是 C# 静态类实际上是 Visual Basic 中的模块而不是 Shared 类(您认为他们会让您在 Visual Basic 中以任何一种方式对其进行编码,但不行).另一个是 VB.NET 确实有自己的 Static 关键字.但是,Static 在 VB.NET 中有不同的含义.

您在 VB.NET 中使用 Static 关键字在函数内声明一个变量,并且当您这样做时,该变量在函数调用之间保持其状态.这与仅在 C# 中声明私有静态类成员不同,因为 VB.NET 中的静态函数成员保证也是线程安全的,因为编译器在编译时将其转换为使用 Monitor 类.

那么为什么要在这里写这些呢?好吧,应该可以构建一个可重用的通用 Iterator 类(或 VB.NET 中的 Iterator(Of T)).在本课程中,您将使用对应于 C# 关键字的 Yield()Break() 方法实现 C# 使用的状态机.然后你可以在函数中使用一个静态实例(在 VB.NET 意义上),这样它最终可以用大约相同数量的代码(丢弃类)完成与 C# 的 yield 几乎相同的工作实现本身,因为它可以无限重复使用).

我不太关心 Yield 来尝试它自己,但它应该是可行的.也就是说,它也远非微不足道,因为 C# 团队成员 Eric Lippert 称之为编译器中最复杂的转换."

自从我一年多前写出第一稿以来,我也开始相信,在 Visual Studio 2010 出现之前,这不可能以一种有意义的方式真正实现,因为它需要向 Iterator 类发送多个 lambda,等等为了真正实用,我们需要 .NET 4 对多行 lambda 的支持.>

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?

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.

解决方案

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#.

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.

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.

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).

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 4's support for multi-line lambdas.

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

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