VB.NET中的迭代器模式(C#会使用产量!)。[英] Iterator pattern in VB.NET (C# would use yield!)

本文是小编为大家收集整理的关于VB.NET中的迭代器模式(C#会使用产量!)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何在 vb.net "> vb.net 中实现迭代图案. yield关键字?

推荐答案

现在在VS 2010 SP1中支持这一点,与异步CTP一起,请参见: MSDN上的迭代器(C#和Visual Basic)下载Visual Studio async CTP(版本3).

这样的代码,有效:

Private Iterator Function SomeNumbers() As IEnumerable
    ' Use multiple yield statements.
    Yield 3
    Yield 5
    Yield 8
End Function

其他推荐答案

vb.net不支持创建自定义迭代器,因此与C#收益率关键字无关.但是,您可能想查看KB文章 如何制作视觉基础.net或Visual basic基本2005类可在每个语句中使用 以获取更多信息.

其他推荐答案

嗯,看起来您可能是不运气:

当将一些C#转换为vb.net时,我今天正在努力解决问题. C#具有一个非常酷的"收益率返回"语句,该语句用于迭代器块中,可为枚举器对象提供值. VB.NET没有"产量"关键字.因此,有一些解决方案(没有真正干净的解决方案)可以解决这个问题.如果您正在循环循环并希望打破枚举器并返回单个值,则可以使用返回语句返回该值.但是,如果您想返回整个枚举,请创建儿童类型的列表()并返回列表.由于您通常使用IEnumerable使用此功能,因此list()会很好地工作.

是一年前写的,不确定从那以后是否有人提出了其他任何事情.


编辑:在VB.NET的版本11(VS2010之后的一个)中,这将是可能的,计划了对迭代器的支持. Spec 本文地址:https://www.itbaoku.cn/post/627642.html

问题描述

How do implement the iterator pattern in VB.NET, which does not have the yield keyword?

推荐答案

This is now supported in VS 2010 SP1, with the Async CTP, see: Iterators (C# and Visual Basic) on MSDN and download Visual Studio Async CTP (Version 3).

Code such as this, works:

Private Iterator Function SomeNumbers() As IEnumerable
    ' Use multiple yield statements.
    Yield 3
    Yield 5
    Yield 8
End Function

其他推荐答案

VB.NET does not support the creation of custom iterators and thus has no equivalent to the C# yield keyword. However, you might want to look at the KB article How to make a Visual Basic .NET or Visual Basic 2005 class usable in a For Each statement for more information.

其他推荐答案

Hmm, looks like you might be out of luck:

I was struggling with an issue today when converting some C# to VB.NET. C# has a really cool "yield return" statement that is used in an iterator block to provide a value to the enumerator object. VB.NET does not have the "yield" keyword. So, there are a few solutions (none of which are really clean) to get around this. You could use a return statement to return the value if you are looping through and would like to break an enumerator and return a single value. However, if you'd like to return the entire enumeration, create a List() of the child type and return the list. Since you are usually using this with an IEnumerable, the List() will work nice.

That was written a year ago, not sure if anyone has come up with anything else better since then..


Edit: this will be possible in the version 11 of VB.NET (the one after VS2010), support for iterators is planned. The spec is available here.