-1

I just realised that is possible to add all the semicolons you want at the end of a line of code and will still compile, like empty instructions. So the following code will compile:

bool foo = true; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;

Is there a reason for that? Why would they accept empty instructions?

lpaloub
  • 868
  • 2
  • 8
  • 21

1 Answers1

0

It's more work to not accept it, and it doesn't hurt anything.


As a side note, you should be able to implement a Visual Studio plugin with the new Roslyn compiler that detects multiple empty statements and then give a compilation error or warning if you wanted to. But as asked in the comments, why would you want to do that? If you're not able to answer that, then that's likely why the creators and maintainers of C# didn't implement a check for that.

mason
  • 31,774
  • 10
  • 77
  • 121
  • 1
    @DavidHeffernan Doing things requires work. There's your citation. I see in your comment above, you said pretty much the same thing too. – mason Oct 16 '14 at 15:59
  • if (foo) bar();; else baz(); Ouch. – Hans Passant Oct 16 '14 at 16:33
  • @HansPassant I'm not sure what your point is. The code you provided doesn't compile, but it's not the same thing as `void Bar(){;;;}` – mason Oct 16 '14 at 16:46
  • Where is the citation? As can be seen in the dupe, there is more to this than you acknowledge. – David Heffernan Oct 16 '14 at 18:24
  • @DavidHeffernan I don't think the dupe is the same issue being presented in this question, though the one that Tim linked to is. There is no citation possible, I was being a little coy when I said that. The point is that allowing multiple empty statements inside an explicit code block doesn't *hurt* anything. And if doesn't hurt anything, why bother preventing it? I can tell you why not: it takes effort to implement, it's something extra to maintain, it will (fractionally) slow the compiler down, and then people will post questions asking why multiple empty statements in a row doesn't work. – mason Oct 16 '14 at 18:28
  • So we disagree. That's fine. – David Heffernan Oct 16 '14 at 18:29
  • @DavidHeffernan I don't think we disagree. We're just looking at how to answer the question differently. I'm answering with rhetorical logic (similar to your asking in comments "why would it work like that?") and I think you would prefer if my answer had some direct quote from Anders Heljsberg or similar. – mason Oct 16 '14 at 18:32