我可以在VB.NET中实现IEnumerable函数的yield return吗?

Cod*_*nis 9 vb.net ienumerable iterator yield-return

可能重复:
VB.NET中的产量

在C#中,当编写返回a的函数时IEnumerble<>,您可以使用yield return返回枚举的单个项目并yield break;表示没有剩余项目.做同样事情的VB.NET语法是什么?

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;
}
Run Code Online (Sandbox Code Playgroud)

将C#转换为VB.NET工具会出现"YieldStatement is unsupported"错误.

Jar*_*Par 13

目前,从语言语法级别来看,VB.Net中没有等效的C#收益率.

然而,Bill McCarthy最近在MSDN杂志上发表了关于如何在VB.Net 9.0中实现类似模式的文章


Cod*_*nis 8

新的Async CTP包括对YieldVB.NET的支持.

有关用法的信息,请参阅Visual Basic中的迭代器.