C# XML Documentation
Deep within the C# compiler is a parser with the great built-in feature of XML documentation. Using the combination of a specialized comment construct of three slashes (///) and built-in documentation tags, XML documentation can be created. The compiler ignores the comments because of the first two slashes, which are standard single-line commenting, and the C# parser looks for the third slash to signify documentation. While the tags used in XML documentation are configurable, the C# standard has predefined a set of tags. The standard tag set has special meanings for the C# compiler, but you can create your own tags to enforce standard documentation and consistent formatting options for your project. The C# compiler only requires that XML comments be well formed. The predefined documentation tags can be subdivided in a couple of different ways. First, I will categorize the tags into two categories: Validated and Nonvalidated.
Validated Tags
The C# compiler validates the syntax of these tags. This is to ensure that the documentation matches the code. Table 5.1 shows the documentation tags that are verified by the C# compiler.
| Documentation Tag | Description |
|---|---|
| <exception> | Apply this tag to a method definition to indicate which exceptions can be thrown. |
| <include> | This tag allows you to specify an XML file that contains the documentation as an alternative to placing the documentation in the code. |
| <param> | Use this tag to describe a parameter of a method. The object browser and intellisense both use this tag to give better information at design time |
| <paramref> | Used in documentation to indicate that you are talking about a parameter. This is not used by intellisense or the object browser but can be used in building documentation to give indication to the reader that you are talking about a parameter to the method. |
| <permission> | Use this tag to describe the accessibility of your method. |
| <see> | This tag lets you specify a link from within documentation text. |
| <seealso> | Documentation that uses this tag appears in the See Also section to direct readers to other related documentation. |
| Warning Number | Description |
|---|---|
| CS1570 | XML comment on "construct" has badly formed XML |
| CS1571 | XML comment on "construct" has a duplicate param tag for "parameter" |
| CS1572 | XML comment on "construct" has a param tag for "parameter", but there is no parameter by that name |
| CS1573 | Parameter "parameter" has no matching param tag in XML comment (but other parameters do) |
| CS1574 | XML comment on "construct" has cref attribute "item" that could not be found |
| CS1580 | Invalid type for parameter "parameter number" in XML comment cref attribute |
| CS1581 | Invalid return type in XML comment cref attribute |
| CS1584 | XML comment on "member" has syntactically incorrect cref attribute "invalid_syntax" |
| CS1587 | XML comment is not placed on a valid language element |
| CS1589 | Unable to include XML fragment "fragment" of file "file"reason |
| CS1590 | Invalid XML include element Missing file attribute |
| CS1591 | Missing XML comment for publicly visible type or member "Type_or_Member" |
| CS1592 | Badly formed XML in included comments file "reason" |
| CS1596 | XML documentation not updated during this incremental rebuild; use /incremental- to update XML documentation |
| CS1598 | XML parser could be not be loaded for the following reason: "reason". The XML documentation file "file" will not be generated |
NOTE
The XML Documentation files for Microsoft's .NET CLR Implementation can be found in [WindowsDir]\Microsoft.NET\Framework\vx.x.xxxx\*.xml for each version of the framework SDK that is installed.Nonvalidated Tags
The documentation tags listed in Table 5.3 are not validated by the compiler. Mostly, these tags are for documentation purposes only.
| Documentation Tag | Description |
|---|---|
| <example> | This tag creates an example of how to use a method to insert into the documentation. |
| <c> | Wrap code in a description within this tag. |
| <code> | This tag indicates multiple lines of code in documentation. |
| <list> | This tag allows you to add lists and tables to your documentation. The listheader tag will add headers to your list, and the item tag enables you to add items to your list. |
| <para> | This tag allows you to structure your documentation into paragraph like groups. |
| <remarks> | The object browser uses this tag to allow you to specify additional information about a class that is additional to the summary section. |
| <returns> | This tag describes what will be returned from a method. |
| <summary> | Use this tag to describe your class. This description will be used by intellisense and the code browser. |
| <value> | This tag allwos you to describe the value of the property. |
C# XML Documentation Tag Reference
Another way of categorizing the tags is to group them again into two groups. Each of these tags will be accompanied with a short code example and brief description. The first group is major tags, which always start an XML comment and are never embedded within another set of tags. There are eleven major documentation tags.
Major Documentation Tags
<example>
The example tag is probably the most-used piece of documentation by users. This is the wonderful piece of code that can be cut and pasted from your documentation into the end users' code. Listing 5.1 shows the XML documentation required to create this example code in your documentation.
Listing 5.1. Example XML Documentation Tag
///<example>
/// <code>
/// int result = Add(1, 2);
// </code>
///</example>
public static int Add(int a, int b)
{
//...
}
<exception>
The exception tag is very important to include with your documentation. Because C# (and most .NET languages, for that matter) does not have a throws keyword like Java, the exception documentation becomes very important. Short of using a decompiler or reflection, there is no easy way of finding out what exceptions a method throws other than the exception documentation tag, as shown in Listing 5.2.
Listing 5.2. Exception XML Documentation Tag
public class DataLib
{
/// <exception cref="System.Data.DataException"></exception>
public static DataSet getCustomers()
{
throw new System.Data.DataException();
// ...
}
}
<include>
In the beginning of the chapter, I mentioned that some developers like to have the documentation with the code to ensure synchronization, but some developers consider well-documented code to be cluttered with XML and find it hard to decipher the code from the comments. This is where the include tag (see Listing 5.3) can help. The include tag allows the C# XML documentation to be in a separate XML file and still be included in the automatic generation of the documentation.
Listing 5.3. Include XML Documentation Tag
/// <include file='xml_documentation_file.xml' path='dev/src/docs' />
public void Test()
{
// ...
}
<param>
The param tag allows you to give the user a little insight into the parameters of a method. Although the code alone should be very readable, this tag can be very useful to a user who may not be thinking along the same lines as the implementer. A good illustration of this is Listing 5.4.
Listing 5.4. Param XML Documentation Tag
/// <param><c>x</c> is a given point</param>
/// <param><c>y</c> is the point to subtract from <c>x</c>.</param>
public void Subtract(Point x, Point y)
{
// ,,,
}
<permission>
Like the exception tag, the permission tag allows you to communicate information to your user that will save him or her valuable time. Again the only way to find out the permissions that a method requires is to use reflection on the Permission Attribute Metadata or to read it in the documentation. Listing 5.5 shows a normal, public method that does not have any special requirements.
Listing 5.5. Permission XML Documentation Tag
/// <permission cref="System.Security.PermissionSet">Anyone can
/// call this method.</permission>
public string HelloWorld()
{
// ...
}
<remarks>
Other than the example tag which gives a user some quick code to cut and paste, the remarks tag will probably be the most read part of your documentation. This is the tag that allows you to describe to your user the purpose of your class, as seen in Listing 5.6.
Listing 5.6. Remarks XML Documentation Tag
/// <remarks>Class <c>Rectangle</c> enables drawing of a
/// rectangle.</remarks>
public class Rectangle
{
// ...
}
<returns>
For publicly available methods, a simple object browser can reveal the return type of the function. However, it is nice for complete documentation to include everything an end user would want, and more importantly, it is very useful when coding with intellisense-enabled IDEs. Listing 5.7 shows the common use of the returns tag.
Listing 5.7. Returns XML Documentation Tag
/// <returns>This method overrides the Equals method of
/// Object and could do a special comparison. It returns if the
/// Objects are Equal or not.</returns>
public override bool Equals(object o)
{
return base.Equals(o);
}
<seealso>
Listing 5.8 demonstrates the seealso tag to direct users to other useful documentation.
Listing 5.8. Seealso XML Documentation Tag
/// <summary>This method determines whether two Rectangles
/// are the same.</summary>
/// <seealso cref="operator=="/>
/// <seealso cref="operator!="/>
public override bool Equals(object o)
{
// ...
}
<summary>
Similar to the remarks tag, a summary (shown in Listing 5.9) gives useful information to the user about the author's intended functionality. Whereas remarks are used at a class level, the summary tag is used on methods and properties of a class.
Listing 5.9. Summary XML Documentation Tag
/// <summary>Logger is a general class to Log information.</summary>
public Logger()
{
///...
}
Minor Documentation Tag
The second grouping of tags is minor tags. In contrast to the major tags, which stand alone, these tags are descriptive tags that are usually embedded within a major tag. There are eleven minor tags.
<c>
The c tag is used to mark code within a textual description. As you can see in Listing 5.10, the c tag is for simple, single line code. For more verbose code examples, use the code tag.
Listing 5.10. C XML Documentation Tag
/// <param><c>x</c> is a given point</param>
/// <param><c>y</c> is the point to subtract from <c>x</c>.</param>
public void subtract(Point x, Point y)
{
// ,,,
}
<code>
The code tag is usually embedded within an example tag for multiple lines of code. As was already mentioned, most users heavily depend upon this. The major difference between an example and code is that code is a subset of an example, which may contain more information than just code. Listing 5.11 illustrates the code tag.
Listing 5.11. Code XML Documentation Tag
///<example>
/// <code>
/// int result = add(1, 2);
// </code>
///</example>
public static int add(int a, int b)
{
//...
}
<list>
A list can really improve the look of your documentation. Listing 5.12 shows how to use, in this case, a numbered list.
Listing 5.12. List XML Documentation Tag
/// <list type="number">
/// <item>
/// <description>Never call this function.</description>
/// </item>
/// <item>
/// <description>There is no guarantee of what it will do.</description>
/// </item>
/// </list>
/// </remarks>
public void Test()
{
// ...
}
<para>
Without formatting, documentation would be much harder to read. Imagine this book without chapters or paragraphs. If documentation is hard to read, then users will not use it and in turn will perceive your product as hard to use. The para tag (Listing 5.13) is used to format your documentation by creating paragraph breaks.
Listing 5.13. Para XML Documentation Tag
/// <summary>Log is a general class to Log information.
/// <para> I find general functions like
this one to be very useful.</para>
///</summary>
public static void Log()
{
// ...
}
<paramref>
The paramref tag in Listing 5.14 is meant as a compliment to the param tag. While the parm tag is used for intellisense and required for robust documentation, the paramref tag is usually found within a summary or remarks tag. This links the user to the parameter, as defined by the param tag.
Listing 5.14. Paramref XML Documentation Tag
/// <summary>This constructor initializes the new Line to
///(<paramref name="a"/>,
/// <paramref name="b"/>).</summary>
public Line(Point a, Point b)
{
// ...
}
<see>
Similar to the seealso tag, the see tag points the readers to more useful documentation. The see tag, however, can be embedded within other tags, as shown in Listing 5.15. Typically, a hyperlink or other method of link is used to allow the users to quickly look at something else; in this case, it's the documentation for the Point class.
Listing 5.15. See XML Documentation Tag
/// <value>Property <c>X</c> represents the Rectangles
/// top left <see cref="Point"/>.</value>
public Point X
{
// ...
}
<value>
The value tag is used to describe a property. The summary tag is typically used to describe a property, but the value tag is used to convey the actual value the property represents.
Listing 5.16. Value XML Documentation Tag
These are the ISO standard tags for XML documentation in C#. Most modern languages support some type of documentation that can be converted into an XML file. For VB.NET users, Microsoft has released a tool as a part of the Visual Studio.NET Powertoys called VB Commenter (http://www.gotdotnet.com/community/workspaces/viewuploads.aspx?id=112b5449-f702-46e2-87fa-86bdf39a17dd). This tool allows VB.NET users to add similar style comments into their code using the triple comment (''') and to use the standard C# tags. Although this is currently a plug-in to Visual Studio.NET, the code is available on the gotdotnet Web site and could be converted into a standalone tool for use with something like Mono's Basic.NET or Sharpdevelop. In addition, it's likely to become a standard part of the language in the next release of .NET.
/// <value>Property <c> TopLeftPoint</c> represents the Rectangles
/// top left <see cref="Point"/>.</value>
public Point TopLeftPoint
{
// ...
}