Yield Return with Null

前端 未结 6 1757
长发绾君心
长发绾君心 2020-12-16 09:52

Is there any way to optionally return a null with a \"return yield\" driven iterator?

I would like to return a null in some cases and I don\'t think this is particul

6条回答
  •  臣服心动
    2020-12-16 10:34

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

     public IEnumerable GetItems() {
         if (something) return null;
         return GetItemsInternal();
     }
    
     private IEnumerable GetItemsInternal() {
         // the actual iterator with "yield return" goes here.
     }
    

提交回复
热议问题