可能是重复的问题:
在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”错误。