When do you use semicolon in C# and why?
Experience Level: Junior
Tags: C#
Answer
In C# there can are two kinds of statements.
- Simple single line statements - this kind of statement requires a semicolon at the end of a statement. Thanks to this you can use multiple simple statements on the same line.
- Compount statements (block statements) - this kind of statement is enclosed in {} brackets and for that reason doesn't need a semicolon at the end of a statement as the end of a statement is marked by the closing curly bracket.
The semicolon isn't the line terminator. It's the statement terminator.
Related C# job interview questions
What is a method parameter in C#?
C# JuniorWhat is a method return type in C#?
C# JuniorWhat is an instance method in C#?
C# JuniorWhat is a static method in C#?
C# JuniorHow can you find out what is the variable type?
C# Junior