Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

ADD COMMENTS TO CODE

C# lets you add comments to your code so you can remind yourself and tell others who view your code what you have done. If you update your code,

comments can tell you what you did when you wrote that string of code. Comments also tell others what you did to the code in case someone else has to review or change your code. In sum, comments show that you are a polite programmer.

Like Java and C++, Visual C# indicates comments with two forward slash marks (//). Visual C# also marks comments in green and automatically adds comments to your code if you

create a new Visual C# project that is something other than an empty project (such as a form or console application). These comments provide information about what certain code does and also include “to do” information about what code you should add to the program skeleton.

If you have only a few words in your comments, you can add the comments to the right of the code line. If you want to say more, you can add multiple lines, although it is usually a good idea to break multi-line comments out onto their own lines so people can read your comments more easily.

ADD COMMENTS TO CODE

Console

Applicatio

¤ Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

Like Java and C++, Visual C# begins its comments with the slash-and-asterisk combination (/*) and ends comments with the asterisk-and-slash combination (*/). However, there are some minor variations. Java and C# have the same comment structure, but C++ is slightly different. In case you want to copy comments from your Java and/or C++ program over to your Visual C# program. Here are examples of comments in Visual C#, Java, and C++.

VISUAL C#

JAVA

C++

//

 

//

 

//

 

/*

comment */

/*

comment */

/*

comment */

Comments

The Class1.cs code appears in the parent window.

Á Delete the comments within the Main method.

Type a decimal value with a two-slash comment immediately afterward.

The comment code color turns green to differentiate your comment from other code.

° Type an integer value and a comment by placing /* at the beginning and an */ at the end of the comment.

The comment code turns green to differentiate your comment from other code.

Note: When you finish typing in an asterisk and slash, your comment appears in boldface; this signifies that you have a complete comment.

· Save the program as the filename.

55

C#

WRITE YOUR FIRST PROGRAM

Your first program lets you become acquainted with programming in the MDE window, gives you confidence in programming with C#, and provides the

enjoyment of seeing your first program compile. The Hello, world! program is the ubiquitous first program that people write no matter what programming language you write the program in. Visual C# is brand new, so this task shows you how to write a Hello, world! program so you can announce your program, and Visual C#, to the rest of the world.

You can program your Hello, world! program in several different ways, though this task shows you only one of them. You can view all sample Hello, world! programs in the

Visual Studio .NET online help available in the MDE window. See page 18 for more information about using Visual Studio .NET online help.

You can also download the Hello, world! samples directly from the Hello World Tutorial page in Visual Studio .NET if you want to open and run them without having to type in all of the variants yourself. After you compile the program, the result appears in a hello.exe file that you can run from Visual C# or by opening the file in Windows and viewing the output in an MS-DOS, for Windows 9x and ME, or console, for Windows NT or 2000, window.

WRITE YOUR FIRST PROGRAM

Console

Applicatio

Click New Project in the

page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

If you are used to programming in C or C++, you will notice some changes in the Visual C# code, including the following:

The program does not use a global Main method.

The program does not support methods and variables at the global level, instead containing those elements within type declarations such as class and struct.

The program does not use :: or -> operators. The former operator does not exist in Visual C# and the latter has limited use.

The program uses a period (.) in compound names, the most common of which is Console.WriteLine.

Because the declaration order is not significant in a Visual C# program, forward declarations do not exist.

The program does not import text by using #include.

Visual C# eliminates some inter-language barriers; for example, the Console class can be written in another language such as Visual Basic.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Class1 class appears

Delete the comments

· Run the program by

Save the program as the

in the parent window.

from the Main method.

pressing the F5 key.

filename.

 

Á Rename Class1 as

 

° Type the

The Hello, world! text

 

 

 

 

Hello.

Console.WriteLine

appears.

 

 

 

 

 

 

statement that announces

 

 

 

 

 

 

 

your program to the world.

 

 

57

Соседние файлы в папке c#