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

corresponding right brace. This indentation scheme helps you remember that all the interior code is part of the namespace.

Adding Summary Comments

Right after the namespace definition, you see three lines that begin with three slashes (///). Lines that begin this way are used to create documentation for your programs. Generally, you leave alone the lines containing <summary> and </summary> and, between these lines, add text that describes your project. This description of your program is stored along with your program. One advantage of C# is that programs are supposed to have some of the documentation built in. Any comments you put between the summary tags will be part of this automatic documentation. Of course, if you don’t add comments, the automatic documentation feature cannot work.

Creating the Class

Class1 defines a class. Essentially, a class is a way of grouping your code. For now you can think of a program as a class because your early programs will have one namespace and one class. As you get more sophisticated, you’ll build namespaces with multiple classes. Classes are the key to C# programming. Right now, the HelloWorld namespace has one class in it, Class1. Actually, the official name of the class is HelloWorld.Class1, but because you are inside the HelloWorld namespace, you don’t have to worry about specifying the namespace. Generally, one of the first things you do when creating a program is rename your class. As a programmer, you get many opportunities to name things. Give your class a name that describes what the program does. Later in this chapter, you will change the class name from Class1 to Hello. Class names in C# usually start with a capital letter.

Like the namespace, a class definition begins a new part of the code and has a pair of braces to denote the new structure.

Trick Whenever you create a new program, be sure to change the name of the class. Although the program will run without changing the name, you will find this confusing later, especially when your programs have a number of classes.

Examining the Main() Method

static void Main(string[] args) begins the Main() method. Any code inside this pair of braces automatically executes when the program is run. For now, all the code in your programs will go inside the Main() method.

Trap Watch your capitalization, especially if you’re accustomed to other C languages. C# uses a capital M in Main, but most other variants of C use a lowercase m.

I will explain later what all the parts of the Main command are, so don’t be intimidated by the string[] args). For now, you don’t need to worry about these details because the editor will build this line for you. You can concentrate, instead, on customizing this code to make it do something interesting.

Examining the Rest of the Code

Inside the Main() method, you see three lines that begin with two slashes (//). Any line that begins with these slashes is a comment. The compiler ignores comments. However, comments are among the most important aspects of good programming. You use comments to document your code—to explain something that’s going on or to make a note to yourself. The comments here tell you where

17

you will write the actual code. You will delete these comment lines and replace them with program code.

You also see a series of right braces. Each of these right braces is vertically aligned with its corresponding left brace. If you don’t include all the right (closing) braces, your program will not work correctly.

Modifying the Code

Although the IDE creates all this code for you, the first part of writing a C# program is to make changes to the code you’re given. You have to make a number of changes right away. Take a look at my modified version of the code:

using System;

namespace HelloWorld

{

///<summary>

///The classic first program in C#

///Andy Harris, 10/01

///</summary>

class Hello

{

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

}// end main

}//end class

}// end namespace

I made a number of small but important changes to the program. First, I added comments to the summary section. At a minimum, you should add comments (to remind yourself what the program is supposed to do), your name, and the date. This might seem like a silly exercise, but it’s a very good habit to form. Note that these summary comments begin with three slashes.

Next, I changed the name of the class from Class1 to Hello. Hello is a much better name for the class because it is more descriptive than Class1.

For the time being, I left the content of the Main() method (the comments with the TODO note in them) alone. I’ll change those soon, but first, there’s some more housekeeping to do.

You might want to add comments after every right (closing) brace because you will have many of these braces in your C# travels, and it’s easy to get confused. Because you use the same character to end a namespace, method, and class definition, figuring out exactly what you intended to end can be a challenge. Not every programmer does this, but I think that it’s a terrific habit to cultivate, especially when you’re getting started.

Writing to the Console

At this point, your program still doesn’t do what it’s supposed to do—greet the user. Now you are ready to change the code in the Main() method. Take out those comment lines and add the following line of code:

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

18

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