3

I stumbled over this piece of code:

public IEnumerable<object> Process()
{
    foreach (var item in items)
    {

        if (item.Created < DateTime.Now)
        {   
            yield return item;
            continue;
        }
    }
}

Can someone help me out understanding why continue isn't needless in this case (VS does not mark continue as a Redundant control flow jump statement)?

CodeNoob
  • 41
  • 1
  • 4

1 Answers1

12

yield return will return an item as part of an enumerator. Once the calling method requests the next item, the code will restart on the line after the yield return.

In this particular case, the continue is redundant, as the loop will do no further work after that point anyway. But as a general application, it has plenty of uses.

Abion47
  • 22,211
  • 4
  • 65
  • 88