Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use single result in While Loop?

Tags:

c#

while-loop

Well, my titling is a bit weird I guess. But let me explain my question.

I have a while loop

while( GetObj() == null || !GetObj().Initialized ){
    doStuff();
}

It would be neat when GetObj() isn't called twice per loop.

I've debugged it in another project to be sure that .NET do not avoid calling the method twice. But that isn't the case.

My actual question now is
Is there a simple way, to avoid calling GetObj() twice in my loop condition?

like image 610
boop Avatar asked Aug 08 '14 21:08

boop


1 Answers

This form is less concise, but possibly more readable.

var obj = GetObj();
while(obj  == null || !obj.Initialized ){
    doStuff();
    obj = GetObj();
}
like image 121
merlin2011 Avatar answered Jan 03 '23 13:01

merlin2011