Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

C H A P T E R 2

Overview of C# Programming

A Simple C# Program

Identifiers and Keywords

Main: The Starting Point of a Program

Whitespace

Statements

Text Output from a Program

Comments: Annotating the Code

15

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

A Simple C# Program

This chapter lays the groundwork for studying C#. Since I’ll use code samples extensively throughout the text, I first need to show you what a C# program looks like and what its various parts mean.

I’ll start by demonstrating a simple program and explaining its components one by one. This will introduce a range of topics, from the structure of a C# program to the method of producing program output to the screen.

With these source code preliminaries out of the way, I can then use code samples freely throughout the rest of the text. So, unlike the following chapters, where one or two topics are covered in detail, this chapter touches on many topics with only a minimum of explanation.

Let’s start by looking at a simple C# program. The complete program source is shown in the top, shaded area in Figure 2-1. As shown, the code is contained in a text file called SimpleProgram.cs. As you read through it, don’t worry about understanding all the details. Table 2-1 gives a line-by-line description of the code.

When the code is compiled and executed, it displays the string “Hi there!” in a window on the screen.

Line 5 contains two contiguous slash characters. These characters—and everything following them on the line—are ignored by the compiler. This is called a single-line comment.

Figure 2-1. The SimpleProgram program

16

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Table 2-1. The SimpleProgram Program, Line by Line

Line Number

Description

Line 1

Tells the compiler that this program uses types from the System namespace.

Line 3

Declares a new namespace, called Simple.

 

The new namespace starts at the open curly brace on line 4 and extends

 

through the matching curly brace on line 12.

 

Any types declared within this section are members of the namespace.

Line 5

Declares a new class type, called Program.

 

Any members declared between the matching curly braces on lines 6 and

 

11 are members that make up this class.

Line 7

Declares a method called Main as a member of class Program.

 

In this program, Main is the only member of the Program class.

 

Main is a special function used by the compiler as the starting point of the

 

program

Line 9

Contains only a single, simple statement; this line constitutes the body of Main.

 

Simple statements are terminated by a semicolon.

 

This statement uses a class called Console, in namespace System, to print

 

out the message to a window on the screen

 

Without the using statement in line 1, the compiler wouldn’t have known

 

where to look for class Console.

 

 

More About SimpleProgram

A C# program consists of one or more type declarations. Much of this book is spent explaining the different types that you can create and use in your programs. The types in a program can be declared in any order. In the SimpleProgram example, only a class type is declared.

A namespace is a set of type declarations associated with a name. SimpleProgram uses two namespaces. It creates a new namespace called Simple, in which it declares its type (class Program), and uses the Console class defined in a namespace called System.

To compile the program, you can use Visual Studio or the command-line compiler. To use the command-line compiler, in its simplest form, use the following command in a command window:

csc SimpleProgram.cs

17

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

In this command, csc is the name of the command-line compiler, and SimpleProgram.cs is the name of the source file.

Identifiers and Keywords

Identifiers are character strings used to name things such as variables, methods, parameters, and a host of other programming constructs that will be covered later.

You can create self-documenting identifiers by concatenating meaningful words into a single descriptive name, using uppercase and lowercase letters (e.g., CardDeck, PlayersHand, FirstName, SocialSecurityNum). Certain characters are allowed or disallowed at certain positions in an identifier. Figure 2-2 illustrates these rules.

The alphabetic and underscore characters (a through z, A through Z, and _) are allowed at any position.

Digits are not allowed in the first position but are allowed everywhere else.

The @ character is allowed in the first position of an identifier but not anywhere else. The use of the @ character, although allowed, is discouraged for general use.

Figure 2-2. Characters allowed in identifiers

Identifiers are case-sensitive. For instance, the variable names myVar and MyVar are different identifiers. It’s generally a bad idea, however, to have identifiers that differ only in the case of some of the letters, because they’re easily confused.

As an example, in the following code snippet, the variable declarations are all valid and declare different integer variables. But using such similar names will make coding more error-prone and debugging more difficult. Those debugging your code at some later time will not be pleased.

// Valid syntactically, but don't do this! int totalCycleCount;

int TotalCycleCount; int TotalcycleCount;

18

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]