我可以在VB.NET中实现IEnumerable函数的yield return吗?
可能重复:
VB.NET中的收益率
在C#中,编写函数时返回 IEnumerble<>
,您可以使用 yield return
返回枚举的单项和 yield break;
表示没有剩余的项目。用于执行相同操作的VB.NET语法是什么?
In C#, when writing a function that returns an IEnumerble<>
, you can use yield return
to return a single item of the enumeration and yield break;
to signify no remaining items. What is the VB.NET syntax for doing the same thing?
来自 NerdDinner 代码:
public IEnumerable<RuleViolation> GetRuleViolations() {
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required","Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description required","Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy required", "HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address required", "Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country required", "Country");
if (String.IsNullOrEmpty(ContactPhone))
yield return new RuleViolation("Phone# required", "ContactPhone");
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
yield break;
}
这将C#转换为VB.NET工具会出现YieldStatement is notpported错误。
This convert C# to VB.NET tool gives a "YieldStatement is unsupported" error.
目前,从语言语法级别来看,VB.Net中没有等效于C#的yield return。
There is currently no equivalent to C#'s yield return in VB.Net from a language syntax level.
然而,最近在MSDN杂志上发表了一篇关于如何在VB.Net 9.0中实现类似模式的MSDN杂志的文章
However there was a recent write up in MSDN magazine by Bill McCarthy on how to implement a similar pattern in VB.Net 9.0
- http://visualstudiomagazine.com/articles/2009/02/01/use-iterators-in-vb-now.aspx