C# - An Introduction - C# Building-Blocks (Page 5 of 9 )
Whenever you write a C# statement, it will be part of a block, skillfully called a “Block of code”. It’s much the same as paragraphs in English, which consist of a number of statements.
It is called a block because it’s consists of related C# statements that will do the job that it’s created for. C# blocks may contain zero, one or many statements. These blocks must be delimited with curly braces “{ }” like the following example:
This is a block of code that contains three statements. There are a number of important points that you must understand here:
int is a keyword and VS.Net will color it blue so that you can differentiate it
There is a semicolon at the end of each of the three statements
Each line contains only one statement
You can have two statements on the same line, simply because the C# compiler knows that you end the statement with the “;”. So you can write the last building block like so:
It’s only apparent that by convention you should write one statement on a line so that your code is readable. You can also nest blocks, meaning you can write one block inside another. Look at the following block:
Again don’t look at the code itself; look only at the blocks. Here we have two nested blocks of code. For now, know that we can create blocks inside other blocks. Later, we will discuss how they can be useful.
Something important to note: When you type the left curly brace “{“, and write your code, then closing it with the right curly brace “}”, you will notice that VS.Net momentarily bolds the entire block. It is nothing more than VS.Net’s way of showing you the contents of that block.
There are three more sections that will discuss code organization.