Why is code with multiple semicolons compiled?

I'm just curious to know why this is allowed.

int i=0;; 

I accidentally typed this. But the program is compiled. I noticed this after many days that I typed ;;

After that I tried to use different characters, for example ~, !, : etc etc

Why it is forbidden, if the first is allowed.

Just interesting to know.

+4
source share
5 answers

You typed an empty statement:

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

It is legal for a statement in C # to not have a body.

From section 8.3 C # Language Specification :

An empty instruction does nothing.

empty statement:
;
An empty statement is used when there are no operations to execute in a context where an instruction is required.

+16
source

An empty expression may be useful. Check out this interesting example of an infinite loop:

  for (;;) { // loops infinitely } 

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

  int count = 0; for (;;) { count++; if (count > 10) break; } Console.WriteLine("Done"); 

But if you really want to make an infinite loop, use while (true) {} instead of for (;;) {} . While (true) less accurate, it is easier to read and readily connects the intention of the cycle endlessly.

+5
source

Optional ; just marks an additional, albeit empty, line of 'C # code. Other characters do not make sense when they are simply placed on their own.

+1
source

C # (and many other languages) use a semicolon to separate statements, rather than caring for line breaks. Empty statements are valid, so you can put as many semicolons as you want. It also means that you can put several statements on one line or split one statement into several lines - the compiler doesn't care.

+1
source

Because a semicolon means the end of an instruction. Others do not. You can have an empty statement.

0
source

All Articles