Why are semicolons necessary at the end of each line in C#? Why can't the complier just know where each line is ended?
Semicolon there is NOT optional.
A "double semicolon" does not have any special meaning in c. The second semicolon simply terminates an empty statement. So you can simply remove it.
Control-flow changing statements like break , continue , goto , return , and throw must have semicolons after them. Declaration statements like function prototypes, variable declarations, and struct/class/union declarations must be terminated with semicolons.
The semi colon is “separater”. The simple answer is to tell the compiler that which line of code is separate each other then you get the desired output otherwise if you not use ; then compiler think all line are one and then confused and gave the many errors and errors.
The line terminator character will make you be able to break a statement across multiple lines.
On the other hand, languages like VB have a line continuation character. I personally think it's much cleaner to terminate statements with a semicolon rather than continue using underscores.
No, the compiler doesn't know that a line break is for statement termination, nor should it. It allows you to carry a statement to multilines if you like.
See:
string sql = @"SELECT foo FROM bar WHERE baz=42";
Or how about large method overloads:
CallMyMethod(thisIsSomethingForArgument1, thisIsSomethingForArgument2, thisIsSomethingForArgument2, thisIsSomethingForArgument3, thisIsSomethingForArgument4, thisIsSomethingForArgument5, thisIsSomethingForArgument6);
And the reverse, the semi-colon also allows multi-statement lines:
string s = ""; int i = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With