Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is continue placed after yield return?

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)?

like image 566
CodeNoob Avatar asked Oct 29 '25 06:10

CodeNoob


1 Answers

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.

like image 63
Abion47 Avatar answered Oct 31 '25 19:10

Abion47