+ 1
semicolon usage
y'all know that after Console.ReadLine() you need to put a semicolon. but in something like Convert.ToInt32() you shouldn't put the semicolon. when you don't have to use? only in another methods?
11 Respuestas
+ 7
ThePretzel
C# makes use of Semicolon to get rid of ambiguity and confusion as its usage makes the code clear, structured and organised.
Like other languages especially C and C++, C# also follows the same rules in the Semicolon application.
The absence of Semicolon throws an error by the compiler which has to be rectified.
It also lets the compiler know the end of the statement.
ref: GeeksForGeeks and C languages
https://www.geeksforgeeks.org/role-of-semicolon-in-various-programming-languages/
This is well documented in all c like languages
+ 5
Console.ReadLine() returns a value, e.g. "32". This returned value then is used by Convert.ToInt32(). We do not want to end the statement after reading the string.
We could write
string x = Console.ReadLine();
int y = Convert.ToInt32(x);
+ 4
We use the semicolon at the end of the line also for Convert:
int x = Convert.ToInt32("32");
Console.WriteLine(x * 2);
Please show a code example where you think a semicolon should be.
+ 3
ThePretzel you should always use semicolons as a general.. unless you are directed otherwise.
+ 3
In C, C++, Java, C# after each statement semicolon is compalsary
+ 3
You pass the string as an argument to the Convert method, you do not want to indicate the end of a statement/ line.
+ 2
C# uses the semicolon as a statement terminator. It marks the end of the statement, and it is part of the statement syntax itself.
In your question, I think I see confusion between what is a statement, and what is an expression. A semicolon is used to terminate a statement, whereas it is not used to terminate an expression. An expression is part of a statement, and usually it evaluates down to a value that is used by the statement.
FYI, some other languages use semicolon as a statement separator where it acts as a placeholder between statements, but it is not part of each statement and is only needed if there are more than one statement on the same line.
0
BroFar but i want to learn when it is and isn't used
0
Lisa i said in the convert method
0
Lisa i meant this:
Convert.ToInt32(Console.ReadLine());
0
Lisa oh aight thanks.