Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolons in C#

Tags:

c#

Why are semicolons necessary at the end of each line in C#? Why can't the complier just know where each line is ended?

like image 208
Loai Abdelhalim Avatar asked Mar 07 '09 14:03

Loai Abdelhalim


People also ask

Is semicolon mandatory in C?

Semicolon there is NOT optional.

What does two semicolons mean in C?

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.

Do you need a semicolon after break in C?

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.

What is the use of semicolon and curly braces in C?

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.


2 Answers

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.

like image 140
mmx Avatar answered Sep 28 '22 11:09

mmx


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; 
like image 32
TheSoftwareJedi Avatar answered Sep 28 '22 10:09

TheSoftwareJedi