Yield Return with Null

If you need to things like that (or throw things like ArgumentException immediately), you need to separate your iterator into two methods:

 public IEnumerable<string> GetItems() {
     if (something) return null;
     return GetItemsInternal();
 }

 private IEnumerable<string> GetItemsInternal() {
     // the actual iterator with "yield return" goes here.
 }

Leave a Comment