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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With