semicolon usage | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

29th Jun 2023, 2:34 PM
Yusuf Ayaz
Yusuf Ayaz - avatar
11 Answers
+ 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
29th Jun 2023, 3:06 PM
BroFar
BroFar - avatar
+ 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);
29th Jun 2023, 3:27 PM
Lisa
Lisa - avatar
+ 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.
29th Jun 2023, 2:49 PM
Lisa
Lisa - avatar
+ 3
ThePretzel you should always use semicolons as a general.. unless you are directed otherwise.
29th Jun 2023, 2:38 PM
BroFar
BroFar - avatar
+ 3
In C, C++, Java, C# after each statement semicolon is compalsary
29th Jun 2023, 2:57 PM
A͢J
A͢J - avatar
+ 3
You pass the string as an argument to the Convert method, you do not want to indicate the end of a statement/ line.
29th Jun 2023, 3:23 PM
Lisa
Lisa - avatar
+ 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.
29th Jun 2023, 6:20 PM
Brian
Brian - avatar
0
BroFar but i want to learn when it is and isn't used
29th Jun 2023, 2:39 PM
Yusuf Ayaz
Yusuf Ayaz - avatar
0
Lisa i said in the convert method
29th Jun 2023, 3:20 PM
Yusuf Ayaz
Yusuf Ayaz - avatar
0
Lisa i meant this: Convert.ToInt32(Console.ReadLine());
29th Jun 2023, 3:24 PM
Yusuf Ayaz
Yusuf Ayaz - avatar
0
Lisa oh aight thanks.
29th Jun 2023, 3:30 PM
Yusuf Ayaz
Yusuf Ayaz - avatar