.net - For Condition VB.NET vs C# -
c#:
static class module1 { public static void main() { (index = 1; index <= getcount(); index++) { console.writeline("for {0}", index); } console.readkey(); } public static int getcount() { console.writeline("getcount"); return 10; } }
result (c# rechecks condition):
getcount 1 getcount 2 getcount 3 getcount 4 getcount 5 getcount 6 getcount 7 getcount 8 getcount 9 getcount 10 getcount
vb.net
module module1 sub main() index = 1 getcount() console.writeline("for {0}", index) next console.readkey() end sub public function getcount() integer console.writeline("getcount") return 10 end function end module
result (vb.net not recheck condition):
getcount 1 2 3 4 5 6 7 8 9 10
a) why doesn't vb.net respect "rule" of recheck condition on each iteration?
b) is there way force vb re-check condition?
c# , vb.net different languages, , similar keywords can have different semantics.
from the docs for ... next
(my emphasis) :
when
for...next
loop starts, visual basic evaluatesstart
,end
, ,step
. this time evaluates these values.
from section 8.8.3 of c# specification (ditto):
when , if control reaches end point of embedded statement (possibly execution of continue statement), expressions of for-iterator, if any, evaluated in sequence, , iteration performed, starting evaluation of for-condition in step above.
if want force condition checked every time, vb.net offers extremely flexible ... loop can have while or until condition, operating @ start or end of loop. statement, loop condition is evaluated every time round.
Comments
Post a Comment