Assuming that the underlying sequence are finite, not infinite, (which you have said is the case) then the loops will not run forever.
Eventually you can be sure that one of the following things will happen:
The
if
will be true for a given item, thus setting the boolean and breaking out of the loop.You will advance to the end of the sequence, thus resulting in
MoveNext
being false.An exception will be thrown from somewhere, such as from the underlying collection being modified by another thread, by a null value in the sequence, or anything else. As you have no try/catch, this will break you out of the loop.
In particular, since each iteration of the loop must advance the iterator (due to MoveNext
) you can be sure that you will eventually end.