15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

54 ❘ ChaPTer 2 cOre c#<br />

(continued)<br />

Tag<br />

<br />

<br />

<br />

<br />

<br />

<br />

desCriPTion<br />

Adds a description for a member.<br />

Documents the return value for a method.<br />

Provides a cross-reference to another parameter. (Syntax is verified by the compiler.)<br />

Provides a “see also” section in a description. (Syntax is verified by the compiler.)<br />

Provides a short summary of a type or member.<br />

Describes a property.<br />

To see how this works, add some XML comments to the MathLibrary.cs file from the previous “More on<br />

Compiling <strong>C#</strong> Files” section. You will add a element for the class <strong>and</strong> for its Add() method, <strong>and</strong><br />

also a element <strong>and</strong> two elements for the Add() method:<br />

// MathLib.cs<br />

namespace Wrox<br />

{<br />

}<br />

///<br />

/// Wrox.Math class.<br />

/// Provides a method to add two integers.<br />

///<br />

public class MathLib<br />

{<br />

///<br />

/// The Add method allows us to add two integers.<br />

///<br />

///Result of the addition (int)<br />

///First number to add<br />

///Second number to add<br />

public int Add(int x, int y)<br />

{<br />

return x + y;<br />

}<br />

}<br />

The <strong>C#</strong> compiler can extract the XML elements from the special comments <strong>and</strong> use them to generate an<br />

XML file. To get the compiler to generate the XML documentation for an assembly, you specify the /doc<br />

option when you compile, together with the name of the file you want to be created:<br />

csc /t:library /doc:MathLibrary.xml MathLibrary.cs<br />

The compiler will throw an error if the XML comments do not result in a well-formed XML document.<br />

This will generate an XML file named Math.xml, which looks like this:<br />

<br />

<br />

<br />

MathLibrary<br />

<br />

<br />

<br />

<br />

Wrox.MathLibrary class.<br />

Provides a method to add two integers.<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!