Semicolons, Positioning, and Indentation


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.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net