1.13. Code Blocks and Variable Scope

C# (like C, C++, and Java) is a block structured language. As mentioned earlier in the chapter, a "block" of code is a series of zero or more lines of code enclosed within braces, like so: {...}.

  • A method declaration, like the Main method of our SimpleProgram, defines a block.

  • A class declaration, like the SimpleProgram class as a whole, also defines a block.

  • As you have seen, many control flow statements also involve defining blocks of code.

Blocks can be nested inside one another to any arbitrary depth:

public class SimpleProgram { // We're inside of the 'class' block (one level deep). static void Main() { // We're inside of the 'Main method' block (two levels deep). int x = 3; int y = 4; int z = 5; if (x ...

Get Beginning C# 2008 Objects: From Concept to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.