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
C# for beginners
C# for beginners

Are you learning C#? Try our test we designed to help you progress faster.

Test yourself