Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

82 C# PRIMER PLUS

A Few Fundamental Observations

The previous sections were able to extract many fundamental C# constructs and mechanisms from Listing 3.1, despite its simple appearance. This section looks at a few general C# concepts by summarizing and building on the knowledge you have gained from Listing 3.1.

C#’s Source Code Format

Blank lines, space characters, tab characters, and carriage returns are collectively known as whitespace. To a large degree, the C# compiler ignores whitespace. Consequently, you can use blank lines, space characters, tab characters, and carriage returns interchangeably. Let’s look at an example. From the C# compilers point of view, the three lines of source code in Figure 3.10 are all identical to line 5 of Listing 3.1.

05: public static void Main()

FIGURE 3.10

Whitespace is ignored by the compiler.

token

public static void Main ()

whitespace (carriage return) whitespace (space character)

Space characters and tab characters can be used interchangeably

token

public

static

void

Main

(

)

whitespace (carriage return) whitespace (tab characters)

Space characters, tab characters, blank lines and carriage returns can be used interchangeably

space characters

space characters

public

blank line

static void

valid but ugly code

Main (

)

carriage return

tab character

Notice how distorted the third line in Figure 3.10 has become; it is still valid code but incomprehensible and ugly.

Chapter 3 • A GUIDED TOUR THROUGH C#: PART I 83

The indivisible elements in a line of source code are called tokens. One token must be separated from the next by whitespace, commas, or semicolons. However, it is unacceptable to separate the token itself into smaller pieces by using whitespace or separators.

A token is a particular instance of a word with a special meaning attached to it. The term token is also frequently used in the scientific fields of logic and linguistics.

Don’t break up tokens with whitespace; this causes invalid code as shown below, which contains three invalid versions of line 5: public static void Main():

pub lic stati c void Main()

public static void Ma

in()

public static vo id Main()

On the other hand, it is possible to aggregate statements and source code on the same line, as shown in the following line.

class Hello { public static void Main() { string answer;

The following is better-styled original code:

02: class Hello 03: {

04:// The program begins with a call to Main()

05:public static void Main()

06:{

07:string answer;

Although C# provides much freedom when formatting your source code, you can increase its clarity considerably by following a reasonable style. It is possible to write valid but ugly source code, as you have seen in a few of the previous examples. Ugly here refers to messy, unclear, confusing source code that is difficult for another person to comprehend.

Listing 3.1 follows a certain style that is adhered to by a large proportion of the programming community. Let’s have a look at a few general guidelines. Line numbers from Listing 3.1 are provided as examples.

Have one statement per line (lines 7, 9, 10, 11, and 14).

However, note the if statement; it’s an exception. It should be spread over several lines (in the case of Listing 3.1 it is spread over two lines).

After an opening brace ({) move down one line and indent (lines 4, and 7).

Indent matching pairs of braces identically (lines 3, 16 and lines 6, 15).

While observing the two previous rules, indent lines between matching pairs of braces identically. (Lines 4, 5 and lines 7–14). Keep in mind that statements after if conditions should be indented (line 13).

84C# PRIMER PLUS

Put in blank lines to separate distinctive logic parts of the source code (line 8).

Do not use whitespace around the parentheses associated with a function name (line 5).

Competent programmers apply other conventions, to improve the style of their source code. We will discuss these in the following chapters whenever relevant.

A Brief Tour Around the .NET Framework

We have already made extensive use of the .NET Framework class library in Listing 3.1. It’s especially visible in lines 9, 10, 11, 13, and 14 with System.Console.WriteLine and System.Console.ReadLine. There is extensive support for equipping your program with a wide variety of functionality from this class library. But how do we know which particular classes and methods we have access to and can reuse and how do we know their characteristics and the functionality they provide? We can find answers to these questions in the comprehensive documentation provided for the class library. The simple aim of this section is to show you how to locate this documentation and confirm the existence of

System.Console.WriteLine and System.Console.ReadLine inside the myriad of other classes and methods.

At the time of writing, the following set of commands can be used to locate the .NET Framework documentation. This might change in later versions of the Software Development Kit (SDK).

Open up the directory with the path D:\Program Files\Microsoft.Net\FrameworkSDK.

Notice that D:\ could be another letter, depending on where your documentation has been installed.

You should then see a window displaying the contents of the FrameworkSDK folder, similar to that shown in Figure 3.11.

FIGURE 3.11

Contents of the

FrameworkSDK folder.

Double-click the StartHere icon to bring up the start page of the .NET Framework Reference. Display the start page of the .NET Framework Documentation by clicking the hyperlink called

Chapter 3 • A GUIDED TOUR THROUGH C#: PART I 85

.NET Framework SDK documentation situated in the Documentation section. Expand the appearing .NET Framework SDK node on the left hand side of the window. Locate the .NET Framework Reference node and expand it. Among many other appearing nodes one is called

.NET Framework Class Library which you can expand to display a window similar to that shown in Figure 3.12.

FIGURE 3.12

The .NET Framework

Class Library

Documentation.

You are now free to browse the documentation for the class library.

To locate the Console class, expand the System node and scroll down to find and expand the Console Class node. You can now find the WriteLine and ReadLine methods by clicking the Console Members node. Click the ReadLine hyperlink in the right part of the window to see the window shown in Figure 3.13.

FIGURE 3.13

Displaying the

Console.ReadLine specifications.