Statements
The statements in C# are very similar to those of C and C++. A statement is a source code instruction describing
a type or telling the program to perform an action.
A simple statement is terminated by a semicolon. (;)
For example, the following code is a sequence of two simple statements. The first statement defines an integer variable named
A simple statement is terminated by a semicolon. (;)
For example, the following code is a sequence of two simple statements. The first statement defines an integer variable named
var1
and initializes its value to 5. The second statement prints the value of variable var1
to a
window on the screen.
int var1 = 5; System.Console.WriteLine("The value of var1 is {0}", var1);
Blocks
A block is a sequence of zero or more statements enclosed by a matching set of curly braces ('
You can create a block from the set of two statements in the preceding example by enclosing the statements in matching curly braces, as shown in the following code:
{ }
') and it acts as a
single syntactic statement.
You can create a block from the set of two statements in the preceding example by enclosing the statements in matching curly braces, as shown in the following code:
{ int var1 = 5; System.Console.WriteLine("The value of var1 is {0}", var1); }
Some important things to know about blocks are the following:
- You can use a block whenever the syntax requires a statement but the action you need requires more than one simple statement.
- Certain program constructs require blocks. In these constructs, you cannot substitute a simple statement for the block.
- Although a simple statement is terminated by a semicolon, a block is not followed by a semicolon. (Actually, the compiler will allow it, because it’s parsed as an empty statement—but it’s not good style.)
Comments
When your code is getting bigger and more people work with your code, you need some inline documentation. Programmers insert comments into
their code to explain and document it.
There are several type of comment available in C#:
There are several type of comment available in C#:
Type | Start | End | Description |
---|---|---|---|
Single-line | // |
The text from the beginning marker to the end of the current line is ignored by the compiler. | |
Delimited | /* |
*/ |
The text between the start and end markers is ignored by the compiler. |
Documentation | /// |
Comments of this type contain XML text that is meant to be used by a tool to produce program documentation. |
0 comments:
Post a Comment