tags:

views:

149

answers:

5

Hi All,

I am just curious to know why this is allowed.

int i=0;;

I accidentaly typed that. But the program compiled. I noticed it after many days that I have typed ;;

After that I tried with different symbols like ~, !, : etc etc

Why is that not allowed where as first one is allowed.

Just curious to know.

+1  A: 

The extra ; just marks out an additional, albeit blank, 'line' of C# code. The other symbols have no meaning when they're just placed on their own.

Will A
A: 

Because the semi-colon signifies the end of a statement. The others do not. You can have a blank statement.

Kevin
+16  A: 

You have typed an empty statement:

int i=0; // that's one statement
; // that's another

It's legal for an statement in C# to have no body.

From section 8.3 of the C# Language Specification:

An empty-statement does nothing.

empty-statement:
;
An empty statement is used when there are no operations to perform in a context where a statement is required.

Michael Petrotta
excellent!!! answer.
A_Var
so does ; on it own get removed by the compiler or does it insert a noop?
Bear Monkey
@Bear: Good question. I don't know, but this line from the same spec section - *Execution of an empty statement simply transfers control to the end point of the statement.* - makes me lean towards a NOP or similar. Only compiled code executes.
Michael Petrotta
@Michael Hmm so if it does insert a NOP it could impact the reliability of locking code such as Monitor.Enter(obj); ; try{ ... } finally{ Monitor.Exit(obj); }.
Bear Monkey
@Bear: How do you figure? Note that, in debug mode, each brace (`{`, `}`) generates a NOP. That's how you can set breakpoints on those braces.
Michael Petrotta
@Michael IIRC I read in an article that the CLR guarantees that no interrupts can occur between Enter and Try for reliable lock release. Im not sure but I think its also said NOPs where not allowed between Enter and Try to enable this behaviour. Can you set a break point between Enter and Try in release mode with the lock statement i wrote? I have no VS on this computer.
Bear Monkey
@Bear: I can't set a breakpoint on *any* empty `;` statement. Hmm. Maybe the compiler is optimizing it out, and maybe you'd be able to write IL directly *with* an empty statement.
Michael Petrotta
@Bear: Sounds like an interesting article. Can you remember where you saw it?
Michael Petrotta
Its not the original article I was reading but Eric talks about the NOP problem I mentioned. I think the article I read was about the bug fix. http://blogs.msdn.com/b/ericlippert/archive/2007/08/17/subtleties-of-c-il-codegen.aspx
Bear Monkey
+5  A: 

The empty statement can actually be useful. Check out this interesting infinite looping example:

    for (;;)
    {
        // loops infinitely
    }

Run the following version as proof, but with break from infinity:

    int count = 0;
    for (;;)
    {
        count++;
        if (count > 10) break;
    }

    Console.WriteLine("Done");

But, if you really want to do an infinite loop, use: while (true) {} instead of for (;;) {}. The while(true) is less terse, easier to read and readily communicates the intent of looping indefinitely.

Paul Sasik
+1  A: 

C# (and a lot of other languages) use the semicolon to separate statements, rather than caring about line breaks. Empty statements are valid, so you can put as many semicolons as you want. It also means you can put multiple statements on one line, or split a single statement across multiple lines - the compiler won't care.

snemarch