Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

This line of code sends the message Hello World to the console, which is another way of saying to the DOS prompt. (Remember, you are beginning with DOS−based programs because they are simpler, but you will graduate to Windows−style programs soon enough.)

On the next line, write the following code:

Console.ReadLine();

This line causes the program to stop and wait until the user presses Enter. If you don’t add this line (or something like it), the program will stop running and disappear before the user can read the greeting.

Trick As soon as you type the period after Console, a list of possible completions appears in the editor window. You can use the arrow keys to look at the entire list and the Tab key to choose the selected element. Because the Console is part of the System namespace, the editor knows all the terms that can be associated with it and gives you an easy way to choose legal terms to finish the statement. When you write the left parenthesis, you see a similar little window explaining which kind of data should go in the parentheses. These little helper windows prevent mistakes by giving you hints on the syntax of C#.

Placing Semicolons Correctly

As you look at the code, you see a semicolon (;) at the end of some lines but not others. You can spot one at the end of the using line and the Console lines but not in the other lines in the program. A pair of braces follows most of the other lines in the program. These indicate that the line begins a structure, such as a namespace, class, or function. The set of braces and whatever they contain are considered a part of that line. Many of the other commands (such as the two I mentioned at the beginning of this paragraph) do not begin a structure. To tell the compiler that you are finished writing a particular command, you must end the line with a semicolon (like writing a period at the end of a sentence to indicate that the sentence is finished).

Most of the time, a semicolon appears at the end of a line of C# code. The only time this is not true is when

The next character is a left brace.

The current character is a right brace.

You are writing one logical line of code on more than one line on the text editor.

Don’t get hung up on memorizing these rules. After you write a few programs, placing semicolons will be a snap.

Moving from Code to a Program

If you don’t get to see it working, your program isn’t really a program, so it’s time to compile your program. Writing programs usually happens in a series of steps: You design the program, write it, test it, and refine it. So far, you’ve designed the program (the design of this program couldn’t get much simpler), and you’ve written it.

Your complete program looks like this:

using System;

19

namespace HelloWorld

{

///<summary>

///The classic first program in C#

///Andy Harris, 10/01

///</summary>

class Hello

{

static void Main(string[] args)

{

Console.WriteLine("Hello, World!"); Console.ReadLine();

}// end main

}//end class

}// end namespace

Now you must test your program.

Compiling Your Program

Ultimately, all that computers can manipulate are binary on and off values—everything the computer does boils down to these elements. Everything the computer knows how to do is expressed in a small list of commands called opcodes that are built in to the hardware of the machine. Even these instructions are expressed in binary form. You can write a program by entering those numbers directly into the computer in binary notation. (In fact, that’s exactly how the first home computer, the Altair, was operated.)

However, this kind of programming, called machine language programming, is tedious and error−prone. The computer can work well with a program in machine language, but writing machine language properly is very difficult for programmers. Computer scientists devised programming languages to make the job easier. Although the syntax of a language such as C# is not much like English, C# is far easier for a programmer to understand and use than machine language. However, computers cannot work directly with the code written in C# or any other high−level language. For the computer to do anything with your program, your program has to be translated (compiled) into machine language.

Fortunately, the Visual Studio IDE makes compiling and running your programs simple. Click the blue arrow that looks like a VCR play button, near the top center of the IDE screen. If all goes well, you will see a black screen that looks like Figure 1.11.

20

Figure 1.11: The Play button compiles and runs your program.

The black screen featured in Figure 1.12 is the console.

Figure 1.12: A cheerful greeting from the Hello World program.

Congratulations! You have written your first program. If you look around on the menu structure created by the C# environment, you will see that you have a HelloWorld.exe file. If you double−click that file, your program will run. (Cool, huh?)

Looking for Bugs

Programs usually do not work on the first try. Many things can go wrong. Simple typing mistakes and errors in logic cause all kinds of problems, even to experienced programmers. Fortunately, if things go wrong, C# has many tools to help you make things right. For example, if I forget to put the semicolon after the Console.WriteLine(); statement in the Hello World program, the editor places a red squiggle at the end of the line (much like the spell checker in Word). When I try to compile the program, I get an error message in the build menu (see Figure 1.13).

21

Соседние файлы в предмете Программирование на C++