XML documentation in C#

By DimitriC at April 05, 2007 19:17
Filed Under: Programming

Next to general commenting, it's possible for a developer to generate XML documentation for his code. Good documentation is a must, more so if your code is going to be used by someone else. So, .NET makes it possible to add documentation in XML-format. You can identify XML-documentation because it's preceded by a tripple slash (///).

//Regular documentation

///XML documentation

There is a set of tags that can be used to help bring structure in the documentation. This set of tags is supported by Visual Studio 2005 who will use these tags for IntelliSense. The most important (and recommended) tags are <param>, <summary> and <remarks>. You can also use the cref attribute to refer to a code element anywhere in the source code.

The <summary>-tag is used for giving a description for the code beneath it. <param> is to list all the parameters used by a method. To be complete, and since you can't take well documented code for granted, you can also specify a <return>-tag. Here you can say what the return value of a method is.

An example:

///<summary>The method adds 2 ints to each other</summary>
///<param name="n1">The first int</param>
///<param name="n2">The second int</param>
///<return>The return value is an int</return>

public static int AddTwoNumbers(int n1, int n2)
{
    int sum = n1 + n2;
    return sum;
}

Now, to create the XML-file, go to the Solution Explorer, right-click on the project, and select "Properties". In the resulting dialog box, select "Build" under the "Configuration Properties" folder. Set the name for the "XML Documentation File". Notice that this must be the same name as the DLL you are creating. Now, rebuild your application.

You can use your DLL-file by referencing to it in your project. When doing so, it will copy the DLL and XML-file to the bin-folder of the project. IntelliSense will now use the XML-file for information about the content of your DLL. You can also view the documentation in Visual Studio's Object Browser.

For more information, I would like to refer to a document written by Anson Horton. In his article you can find a list of all the available tags, including examples.

Comments are closed