c# - Empty Semicolon and Open Closed Curly Brace is Working -
i have console application project support assigned me, decide conduct code review in class, , while i'm doing code review run line contain semicolon , other line empty open-close curly brace, expectation compiler throw error, tried run console application working fine , no sign of error, thought compiler have problem decide reinstall it, console app still working fine, decide create new method test semicolon , curly brace try make nested curly brace , console.writeline()
inside of , surprisingly working. can explain why happening or it's me , compiler?
static void testmethod() { ; { } ; { } { ; } { }; { { { { { { { console.writeline("hello, world!"); console.read(); } } } } } } } }
empty statements allowed in c#.
an empty statement used when there no operations perform in context statement required.
execution of empty statement transfers control end point of statement. thus, end point of empty statement reachable if empty statement reachable.
also note the definition of block. statements inside block optional.
a block consists of optional statement-list (section 8.2.1), enclosed in braces. if statement list omitted, block said empty.
also see comments "feature" eric lippert. (as mentioned @kobi in comments above.)
note: people cite examples such code reason have empty statement:
while (readnextchar() == ' ') // skip spaces ;
or horrific:
while (readnextchar() == ' '); // skip spaces
but i'd happy if had written as:
while (readnextchar() == ' ') { // skip spaces }
however, illustrate reason might need empty blocks (which have no issue with).
Comments
Post a Comment