Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do some lines not have semicolon in C#?

Tags:

c#

I am just trying to figure out the technical reason why in the below some lines do not end with a semicolon but other lines do - what is it about a semicolon that C# expects in some lines then others....

In fact whilst writing this I noticed that the statements that have to have curly brackets {} do not need semicolons but the lines that are on its own "Console.WriteLine" do need it.

Really trying to find the technical reasons for this...

ie:

namespace checkPackage     **//no semicolon**
{
    class Program      **//no semicolon**
    {
        static void Main(string[] args)     **//no semicolon**
        {
            listFilesInDirectory(@"C:\Temp\");    **//NEEDS a semicolon**
        }

        static void listFilesInDirectory(string workingDirectory)   **//no semicolon**
        {
            string[] filePaths = Directory.GetFiles(workingDirectory);  **//NEEDS a semicolon**

            foreach (string filePath in filePaths)   **//no semicolon**
            {
                Console.WriteLine(filePath);  **//NEEDS a semicolon**
            }

        }
    }
}
like image 553
lara400 Avatar asked Nov 07 '14 14:11

lara400


People also ask

Are semicolons required in C?

Slightly aged languages (some would call them business standards), such as C/C++, Java, C#, require you to put a semicolon at the end of every statement.

What if semicolon is not used in C?

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.

Which of the following does not end with semicolon in C?

when including libraries c, the line does not end with a semicolon, while other statements do.

Why semicolon is not used in while loop?

When you first start working with while statements, you might accidentally place a semicolon after the “while(true/false expression)” part of the statement such as shown below. The semicolon results in a null statement, a statement that does nothing.


3 Answers

The semi-colon isn't a line terminator... it's a statement terminator. There's a semi-colon at the end of every expression statement and declaration statement.

(if, for etc statements aren't expression or declaration statements.)

So for example:

public class Foo // Part of a class declaration
{
    int x = 0; // Declaration statement

    public void Bar() // Part of a method declaration
    {
        Console.WriteLine(x); // Expression statement (using an invocation expression)
    } // End of the method declaration, but not a declaration statement 

} // End of class declaration, but not a declaration statement

The purpose of requiring them is so that the compiler can tell when you wanted to end the statement instead of continuing on the next line:

 int x = 5;
 int y = x // This isn't the end of the statement!
         + 5; // This is...

One alternative (used by VB, for example) is to have a line continuation where you want to explicitly continue onto the next line even though the current line would be a valid statement.

As noted in comments, the do statement is an anomaly here. I see no obvious reason why this shouldn't be valid:

do { } while (false)

... but it isn't. It may be related to the fact that the plain while statement needs a statement body, e.g. while (true); (empty statement) or while (true) {} (block statement). The closest I can come is "because the C# specification says the do statement needs a semi-colon at the end..."

like image 92
Jon Skeet Avatar answered Oct 05 '22 23:10

Jon Skeet


The reason? Because the spec says so:

A statement can consist of a single line of code that ends in a semicolon, or a series of single-line statements in a block. A statement block is enclosed in {} brackets and can contain nested blocks.

The foreach definition is followed by a statement, which must either be enclosed in {} or end on a semicolon. This statement is then referred to as the loops body.

This might not be an adequate answer to the question "why". To go deeper into this, you'd really need to ask the designers. I suppose, they wanted to make it close to C-Syntax, as opposed to other languages where the line-break serves a similar purpose. (Python iirc)

If you want a more formal definition, download the C# Language Specification and read section 1.2 to 1.5.

like image 39
DasKrümelmonster Avatar answered Oct 05 '22 23:10

DasKrümelmonster


There are two kinds of statements, simple statements and compound statements. The loops and method declarations are compound statements. Each simple statement end with semicolon but compound statements don't end with semicolon. From msdn

A compound statement consists of zero or more statements enclosed in curly braces ({ }). A compound statement can be used anywhere a statement is expected. Compound statements are commonly called "blocks."

The curly braces specifies the begin and the end of a compound statement.So compiler looks for the braces instead of the semicolon to determine the beginning and the end of the statement.

like image 36
Selman Genç Avatar answered Oct 06 '22 00:10

Selman Genç