可能的重复:
VB.NET 中的 Yield
在 C# 中,当编写一个返回 的函数时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;
}
此将 C# 转换为 VB.NET 工具会给出“不支持 YieldStatement”错误。