Empty semicolon no error?

20

I was working on a project and unintentionally ran into the semicolon that ended up being inserted well after a if . I was intrigued because Visual Studio did not point as an error, and when trying to compile the project I was successful.

The code looks like this:

if(alguma_coisa)
{
   //Bloco lógico
};

Motivated by curiosity, I realized that you can also enter a semicolon at the end of a loop like while , for , and foreach .

I showed it to a friend and he revealed the possibility of inserting the semicolon several times in sequence on the same line as the following:

if(alguma_coisa)
{
   //Bloco lógico
}; ; ; ; ; ;

And yet the project is compiled without any problem.

  

Should not a compile error be generated?
  Why is this allowed?

    
asked by anonymous 08.12.2015 / 18:33

6 answers

27

A semicolon is used to determine the end of an instruction. What happens in this case is that "the statement" does nothing.

Do this:

if(true)
{
};

It's the same thing as doing

if(true)
{    
}
; //Final da "instrução" que está vazia
    
08.12.2015 / 18:36
17

This is a perfectly valid yet innocuous syntax. Each semicolon represents the end of a statement ; without any content, the block is simply ignored when compilation occurs.

    
08.12.2015 / 18:35
13

Between the closing of if which is } and ; inserted is a block of code that does nothing. Same thing between ; and other ;

    
08.12.2015 / 18:43
10

At the time of the question I understood the answers as certain and I voted for them. But they are talking about the consequence and not the cause.

It is true that it seems that the compiler cleverly eliminates the ; that is of no practical use, it is true that this character is often used as a statement terminator. Everything that is answered is right and works, but it did not talk because it works.

But here you have a empty statement . This in the language specification . And there is warning when it can be had unintentionally used .

The jbueno response example shows that what looks like a terminator is actually just a empty statement on the same line. Then

if(true)
{
};

is really the same as

if(true)
{    
}
;

So it is not a terminator for the block of if , it is another statement completely different and does nothing.

    
11.07.2016 / 14:25
10

I think the question has already been answered well, but there is one case that I think it is important to mention here.

In the same way that you can have semicolons without causing compilation errors, the following code also does not cause compilation errors

{
    //bloco lógico
}

See that it is a block between { } with no logical construct, not an if, for, while, or any method.

The compiler allows me to do this and it separates the context of variables if they are declared, for example

{
    var i = 1;
    Console.Write(i); //ok
}
Console.Write(i); //erro de compilação, i não existe no contexto atual

But then what does this have to do with ; ? In your first example, you placed a semicolon after closing the block of if , but imagine that you did not want to do that yourself

if(alguma_coisa);
{
   //Bloco lógico
}

There would be no compilation error, what would happen during execution is that this Bloco lógico would always run, regardless of the if , and I think this is one of the most common problems that can happen with a point and comma added by mistake.

Another interesting case is also a for

int i;
for(i = 0; i < 10; i++);
{
    Console.Write(i);
}

Then you would see in the console only one value, the 10, which should never appear.

But this error is less common since it is usually declared the direct variable in for, and in this case a compilation error would occur

for(int i = 0; i < 10; i++);
{
    Console.Write(i); //erro de compilação, i não existe no contexto atual
}
    
02.03.2016 / 14:37
8

The C # compiler - Roslyn, in the most current version - has "intelligence" to fill these situations.

  • Multiple semicolons: return; ; ; = > return;
  • Concatenation of strings: string a = "b" + "c"; = > var a = string.Concat("b", "c");
  • Surplus virgula: var a = new int[] {1,2,3,}; = > var a = new[]{1,2,3};

Including deletion of declared and unused variables:

var a = UmMetodoQualquer(); = > UmMetodoQualquer();

That's why there are compilation errors in these cases, because the C # compiler looks at and does these little "fixes."

    
02.03.2016 / 13:26