Is yield useful outside of LINQ?

后端 未结 14 954
野性不改
野性不改 2020-12-24 06:44

When ever I think I can use the yield keyword, I take a step back and look at how it will impact my project. I always end up returning a collection instead of yeilding becau

14条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 07:38

    At a previous company, I found myself writing loops like this:

    for (DateTime date = schedule.StartDate; date <= schedule.EndDate; 
         date = date.AddDays(1))
    

    With a very simple iterator block, I was able to change this to:

    foreach (DateTime date in schedule.DateRange)
    

    It made the code a lot easier to read, IMO.

提交回复
热议问题