Subscribe

RSS Feed (xml)

Semicolons, Positioning, and Indentation in C#

As mentioned earlier, the semicolon signals the end of a statement. That is, each individual statement must end with a semicolon. A block, however, is not terminated with a semicolon. Since a block is a group of statements, with a semicolon after each statement, it makes sense that a block is not terminated by a semicolon; instead, the end of the block is indicated by the closing brace.

C# does not recognize the end of the line as the end of a statement—only a semicolon terminates a statement. For this reason, it does not matter where on a line you put a statement. For example, to C#,

x = y;
y = y + 1;
Console.WriteLine(x + " " + y);

is the same as

x = y;  y = y + 1;  Console.WriteLine(x + " " + y);

Furthermore, the individual elements of a statement can also be put on separate lines. For example, the following is perfectly acceptable:

Console.WriteLine("This is a long line of output" +
                  x + y + z +
                  "more output");

Breaking long lines in this fashion is often used to make programs more readable. It can also help prevent excessively long lines from wrapping.

You may have noticed in the previous examples that certain statements were indented. C# is a free-form language, meaning that it does not matter where you place statements relative to each other on a line. However, over the years, a common and accepted indentation style has developed that allows for very readable programs. This book follows that style, and it is recommended that you do so as well. Using this style, you indent one level after each opening brace and move back out one level after each closing brace. There are certain statements that encourage some additional indenting; these will be covered later.

No comments:

Post a Comment

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail