我可以在VB.NET中实现IEnumerable函数的yield return吗? [英] Can I implement yield return for IEnumerable functions in VB.NET?

查看:396
本文介绍了我可以在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

这篇关于我可以在VB.NET中实现IEnumerable函数的yield return吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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