for loop - How do I implement for-else and foreach-else statement in C#? -
i know not exist, there alternatives? this:
for (int = 0; !connected && < 100; i++) { thread.sleep(1); } else throw new connectionerror();
the python construct foreach-else this:
foreach( elem in collection ) { if( condition(elem) ) break; } else { dosomething(); }
the else executes if break not called during foreach loop
a c# equivalent might be:
bool found = false; foreach( elem in collection ) { if( condition(elem) ) { found = true; break; } } if( !found ) { dosomething(); }
source: the visual c# developer center
Comments
Post a Comment