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

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Comments: Annotating the Code

You’ve already seen single-line comments, so here I’ll discuss the second type of inline comments— delimited comments—and mention a third type called documentation comments.

Delimited comments have a two-character start marker and a two-character end marker.

Text between the matching markers is ignored by the compiler.

Delimited comments can span any number of lines.

For example, the following code shows a delimited comment spanning multiple lines.

↓ Beginning of comment spanning multiple lines

/*

This text is ignored by the compiler.

Unlike single-line comments, delimited comments like this one can span multiple lines.

*/

↑ End of comment

A delimited comment can also span just part of a line. For example, the following statement shows text commented out of the middle of a line. The result is the declaration of a single variable, var2.

Beginning of comment

int /*var 1,*/ var2;

End of comment

Note Single-line and delimited comments behave in C# just like they do in C and C++.

27

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

More About Comments

There are several other important things you need to know about comments:

Nested delimited comments are not allowed. Only one comment can be in effect at a time. If you attempt to nest comments, the comment that starts first is in effect until the end of its scope.

The scope for particularly comment types is as follows:

For single-line comments, the comment is in effect until the end of the current line.

For delimited comments, the comment is in effect until the first end delimiter is encountered. The following attempts at comments are incorrect:

Opens the comment

/* This is an attempt at a nested comment. /* ← Ignored because it’s inside a comment

Inner comment

 

*/ ←Closes the comment because it’s the first end delimiter encountered

*/

←Syntax error because it has no opening delimiter

Opens the comment

↓ Ignored because it’s inside a comment

//

Single-line comment

/* Nested comment?

*/ ← Incorrect because it has no opening delimiter

Documentation Comments

C# also provides a third type of comment: the documentation comment. Documentation comments contain XML text that can be used to produce program documentation. Comments of this type look like single-line comments, except that they have three contiguous slashes rather than two. I’ll cover documentation comments in Chapter 25.

The following code shows the form of documentation comments:

///<summary>

///This class does...

///</summary>

class Program

{

...

28

CHAPTER 2 OVERVIEW OF C# PROGRAMMING

Summary of Comment Types

Inline comments are sections of text that are ignored by the compiler but are included in the code to document it. Programmers insert comments into their code to explain and document it. Table 2-5 summarizes the comment types.

Table 2-5. Comment Types

Type

Start

End

Description

Single-line

//

 

The text from the beginning marker to the end of the current line is

 

 

 

ignored by the compiler.

Delimited

/*

*/

The text between the start and end markers is ignored by the

 

 

 

compiler.

Documentation

///

 

Comments of this type contain XML text that is meant to be used

 

 

 

by a tool to produce program documentation.

 

 

 

 

29

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