15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Comments ❘ 53<br />

This displays as .23, because the # symbol is ignored if there is no character in that place, <strong>and</strong> zeros will<br />

either be replaced by the character in that position if there is one or be printed as a zero.<br />

using CommenTs<br />

The next topic — adding comments to your code — looks very simple on the surface, but can be complex.<br />

internal Comments within the source files<br />

As noted earlier in this chapter, <strong>C#</strong> uses the traditional C-type single-line (//...) <strong>and</strong> multiline (/* ... */)<br />

comments:<br />

// This is a singleline comment<br />

/* This comment<br />

spans multiple lines. */<br />

Everything in a single-line comment, from the // to the end of the line, will be ignored by the compiler,<br />

<strong>and</strong> everything from an opening /* to the next */ in a multiline comment combination will be ignored.<br />

Obviously, you can’t include the combination */ in any multiline comments, because this will be treated as<br />

the end of the comment.<br />

It is actually possible to put multiline comments within a line of code:<br />

Console.WriteLine(/* Here's a comment! */ "This will compile.");<br />

Use inline comments with care because they can make code hard to read. However, they can be useful when<br />

debugging if, say, you temporarily want to try running the code with a different value somewhere:<br />

DoSomething(Width, /*Height*/ 100);<br />

Comment characters included in string literals are, of course, treated like normal characters:<br />

string s = "/* This is just a normal string .*/";<br />

Xml documentation<br />

In addition to the C-type comments, illustrated in the preceding section, <strong>C#</strong> has a very neat feature that<br />

we want to highlight: the ability to produce documentation in XML format automatically from special<br />

comments. These comments are single-line comments but begin with three slashes (///) instead of the usual<br />

two. Within these comments, you can place XML tags containing documentation of the types <strong>and</strong> type<br />

members in your code.<br />

The tags in the following table are recognized by the compiler.<br />

Tag<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

desCriPTion<br />

Marks up text within a line as code, for example int i = 10;.<br />

Marks multiple lines as code.<br />

Marks up a code example.<br />

Documents an exception class. (Syntax is verified by the compiler.)<br />

Includes comments from another documentation file. (Syntax is verified by<br />

the compiler.)<br />

Inserts a list into the documentation.<br />

Marks up a method parameter. (Syntax is verified by the compiler.)<br />

Indicates that a word is a method parameter. (Syntax is verified by the compiler.)<br />

Documents access to a member. (Syntax is verified by the compiler.)<br />

continues<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!