我可以在 VB.NET 中为 IEnumerable 函数实现收益返回吗? [英] Can I implement yield return for IEnumerable functions in VB.NET?
问题描述
可能重复:
在 VB.NET 中生成
在C#中,在编写返回IEnumerble<>
的函数时,可以使用yield return
返回枚举的单项,yieldbreak;
表示没有剩余项目.做同样事情的 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;
}
这个convert C# to VB.NET tool提供了一个YieldStatement 不受支持"错误.
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.
但是,Bill McCarthy 最近在 MSDN 杂志上发表了一篇关于如何在 VB.Net 9.0 中实现类似模式的文章
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
这篇关于我可以在 VB.NET 中为 IEnumerable 函数实现收益返回吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!