Click here to Skip to main content
11,431,829 members (64,440 online)
Click here to Skip to main content

C# Documenting and Commenting

, 20 Jan 2003
Rate this:
Please Sign up or sign in to vote.
Use C# Comment tags to produce great documentation and code

Introduction

Most of us will have experienced the dread of updating documentation at some point or other. C# and Visual Studio .NET (VS.NET) give us the ability to maintain code and documentation in the same file, which makes the whole process a lot easier. VS.NET does this by taking specially marked and structured comments from within the code and building them into an XML file. This XML file can then be used to generate human-readable documentation in a variety of forms including web pages, MSDN style documentation and Intellisense within the code window.

Configure XML Commenting

VS.NET produces XML comments by taking specially marked and structured comments from within the code and building them into an XML file. This XML file can then be used to generate human-readable documentation in a variety of forms including web pages, MSDN style documentation and Intellisense within the code window. The first thing you need to do is enable the XML commenting feature for your VS.NET project.

  1. Right Click on the project in the solution explorer and select "Properties".
  2. Within the properties dialog double click on the “Configuration Properties” node.
  3. The Build node should be already selected and you should be able to see the “XML Documentation File” entry under “Outputs”. Here is where you must enter the name of the XML file that will contain the comment data. You can call the file what you like, but for compatibility with all the features of XML commenting, it should take the form of MyAssemblyName.Xml e.g. Adjuster.BusinessServices.dll has a related XML file called Adjuster.BusinessServices.Xml

With this enabled, your XML comment data file will be rebuilt each time you build your project. Any problems that occur when trying to generate the file will not prevent a build but will be flagged in the VS.NET Task List. Assuming you do not have compile warnings set to errors.

VS.NET Task List flagging XML commenting error.

With that enabled you can start to use the special XML tags in your procedure “headers”. To get you started, place the cursor on the line directly above a procedure’s definition. Once there, press the “/” key three times, this will automatically insert a summary tag into your code. If the procedure had any arguments there should now be a param tag for each one.

/// <summary>
/// 
/// </summary>
/// <param name="data"></param>
public void SaveData(ref DataSet data)
{

}

The SaveData code above is what is inserted as default

/// <summary>
/// Connects to the database and attempts to apply 
/// all adds, updates and deletes
/// </summary>
/// <param name="data">a dataset, passed by reference, 
/// that contains all the 
/// data for updating>/param>
public void SaveData(ref DataSet data)
{

}

This SaveData code is after I have added my comments describing what the routine does in the summary tag and what the data parameter is. This very simple action has given us enough to provide basic documentation including intellisense just like that provided by the .NET Framework assemblies.

It is clear from just this feature, how useful XML commenting is. When you include a reference to a .NET project that has XML commenting enabled, the XML documentation file we named earlier is copied over along with the binary to the current project’s \bin directory. This gives you the intellisense across assemblies.

The summary tag is the most basic of tags. The list below is the complete set currently supported by VS.NET. The ones marked with a * are the ones I feel are the most useful and the ones we will be dealing in the following examples.

  • c
    The c tag gives you a way to indicate that text within a description should be marked as code. Use code to indicate multiple lines as code.

  • code*
    The code tag gives you a way to indicate multiple lines as code. Use <c> to indicate that text within a description should be marked as code.

  • example*
    The example tag lets you specify an example of how to use a method or other library member. Commonly, this would involve use of the code tag.

  • exception*
    The exception tag lets you specify which exceptions a class can throw.

  • include
    The include tag lets you refer to comments in another file that describe the types and members in your source code. This is an alternative to placing documentation comments directly in your source code file.

  • para
    The para tag is for use inside a tag, such as <remarks> or <returns>, and lets you add structure to the text.

  • param*
    The param tag should be used in the comment for a method declaration to describe one of the parameters for the method.

  • paramref
    The paramref tag gives you a way to indicate that a word is a parameter. The XML file can be processed to format this parameter in some distinct way.

  • permission*
    The permission tag lets you document the access of a member. The System.Security.PermissionSet lets you specify access to a member.

  • remarks*
    The remarks tag is where you can specify overview information about a class or other type. <summary> is where you can describe the members of the type.

  • returns
    The returns tag should be used in the comment for a method declaration to describe the return value.

  • see
    The see tag lets you specify a link from within text. Use <seealso> to indicate text that you might want to appear in a See Also section.

  • seealso*
    The seealso tag lets you specify the text that you might want to appear in a See Also section. Use <see> to specify a link from within text.

  • summary*
    The summary tag should be used to describe a member for a type. Use <remarks> to supply information about the type itself.

  • value*
    The value tag lets you describe a property. Note that when you add a property via code wizard in the Visual Studio .NET development environment, it will add a <summary> tag for the new property. You should then manually add a <value> tag to describe the value that the property represents.

MSDN Style Documentation and NDOC

We have taken the intellisense format as far as it will go, but there is much more we can do with MSDN style documentation. There is a tool that comes with VS.NET that you will find at “Tools|Build Comment Web Pages…” which will take your C# XML comments from source files and generate linked HTML files. This comes straight out of the box so should not be totally disregarded. But if you want to create easy-to-use, helpful, cross-referenced and attractive documentation, then I can strongly recommend the free, open source tool NDoc. The screenshot below is taken from a compiled help file produced from NDoc and is an example of the quality it can produce.

The two routines below will show the correct usage for most of the XML comment tags we saw earlier. The cref attribute of the exception tag is used for cross-referencing to an Exception type. This attribute is also used in the seealso, permission and see tags to reference a type. The type must be available from the current compilation environment. The compiler checks that the referenced type exists and passes relevant data to the output XML.

/// <summary>
/// Gets or sets the age of the person involved in the accident
/// </summary>
/// <value>Age of the claimant.</value>
/// <remarks> The value must be numeric.
/// </remarks>
/// <exception cref="System.ApplicationException">Thrown when a non-
/// numeric value is assigned.</exception>
public string Age
{
}

This Age property once processed by NDoc will produce this.

I have drawn attention to areas in the picture and their corresponding XML comment tags.

/// <summary>
/// Connects to the database and attempts to apply all adds, 
/// updates and deletes
/// </summary>
/// <seealso cref="Adjuster.BusinessServices.Accident"/> 
/// <param name="data">a dataset, passed by reference, 
/// that contains all the 
/// data for updating</param>
/// <example> This sample shows how to call the SaveData 
/// method from a wireless device.
/// <code>
/// 
///AccidentCRUD accCRUD = new Adjuster.BusinessServices.AccidentCRUD();
///accCRUD.SaveData(ref ds);
///
///</code>
///</example>
///<permission cref="System.Security.PermissionSet">Everyone 
///can access this method.</Permission>
public void SaveData(ref DataSet data)
{
}

This SaveData method once processed by NDoc will produce this

Again I have drawn attention to areas in the picture and their corresponding XML comment tags. The Accident cross-reference in the “See Also” section is the only one that I added. By default NDoc adds cross-referencing for the parent class, the parent class’ members and the parent class’ namespace.

With the combination of NDoc and VS.Net & C#’s ability to produce these comments you can get great technical documentation at a level so close to the code, that there is absolutely no excuse for it not telling it as it is.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Patrick Long
Web Developer
Europe Europe
I have been programming now for 10 years starting with COBOL, CICS and DB2 and ending up with ASP.NET, WPF, and various web technologies.

I work for Munkiisoft in the UK.

Check out my blog here

Comments and Discussions

 
QuestionHow document an Item in enum object? Pin
ReneTrevi11-Mar-15 10:20
memberReneTrevi11-Mar-15 10:20 
AnswerRe: How document an Item in enum object? Pin
phil.o11-Mar-15 10:51
professionalphil.o11-Mar-15 10:51 
GeneralRe: How document an Item in enum object? Pin
ReneTrevi11-Mar-15 13:31
memberReneTrevi11-Mar-15 13:31 
GeneralRe: How document an Item in enum object? Pin
phil.o11-Mar-15 17:37
professionalphil.o11-Mar-15 17:37 
QuestionWhat is the value of the value tag? Pin
George Henry 195430-Aug-13 7:27
memberGeorge Henry 195430-Aug-13 7:27 
QuestionNice article Pin
Pavel Vladov14-Nov-12 4:53
memberPavel Vladov14-Nov-12 4:53 
GeneralCommercial option for .NET documentation Pin
Richard Slogget5-Oct-12 13:31
memberRichard Slogget5-Oct-12 13:31 
Questionhow can i use this dll in my code Pin
sushilabhanvar23-Jun-09 5:40
membersushilabhanvar23-Jun-09 5:40 
GeneralIs there a way to document overloaded methods Pin
cpotting30-May-08 13:16
membercpotting30-May-08 13:16 
General[Message Removed] Pin
stonber1-Oct-08 8:50
memberstonber1-Oct-08 8:50 
Spam message removed
GeneralRe: Is there a way to document overloaded methods Pin
cpotting2-Oct-08 4:16
membercpotting2-Oct-08 4:16 
GeneralRe: Is there a way to document overloaded methods Pin
deepakpaggi25-Sep-09 0:13
memberdeepakpaggi25-Sep-09 0:13 
GeneralRe: Is there a way to document overloaded methods Pin
piyushmavani13-Aug-10 1:26
memberpiyushmavani13-Aug-10 1:26 
GeneralGreat job. Also SandCastle instead of NDoc Pin
Reve10115-Nov-07 11:55
memberReve10115-Nov-07 11:55 
QuestionC# properties view Pin
hanseh16-Oct-07 21:05
memberhanseh16-Oct-07 21:05 
AnswerRe: C# properties view Pin
Patrick Long16-Oct-07 22:07
memberPatrick Long16-Oct-07 22:07 
QuestionRe: C# properties view Pin
hanseh16-Oct-07 23:48
memberhanseh16-Oct-07 23:48 
GeneralThanks a million Pin
bigblockfw14-Apr-07 15:32
memberbigblockfw14-Apr-07 15:32 
GeneralXML File Copying Problem Pin
Navaneethkn19-Jan-07 0:21
memberNavaneethkn19-Jan-07 0:21 
GeneralRe: XML File Copying Problem Pin
Patrick Long19-Jan-07 12:09
memberPatrick Long19-Jan-07 12:09 
GeneralRe: XML File Copying Problem Pin
Navaneethkn5-Feb-07 23:32
memberNavaneethkn5-Feb-07 23:32 
QuestionNice article, Pin
abdulmalikm5-Dec-06 4:23
memberabdulmalikm5-Dec-06 4:23 
GeneralProblem documenting reference parameter Pin
Ariston Darmayuda14-May-06 22:11
memberAriston Darmayuda14-May-06 22:11 
QuestionEnum member descriptions? Pin
Anonymous13-Sep-05 10:41
sussAnonymous13-Sep-05 10:41 
AnswerRe: Enum member descriptions? Pin
Alexander Lowe16-Sep-05 9:10
sussAlexander Lowe16-Sep-05 9:10 
GeneralDLL comments Pin
dabuskol7-Aug-05 2:14
memberdabuskol7-Aug-05 2:14 
GeneralRe: DLL comments Pin
dabuskol7-Aug-05 2:37
memberdabuskol7-Aug-05 2:37 
GeneralError when I used NDoc Pin
sreenaths@mailcity.com16-Dec-04 7:24
membersreenaths@mailcity.com16-Dec-04 7:24 
GeneralRe: Error when I used NDoc Pin
Patrick Long16-Dec-04 10:16
memberPatrick Long16-Dec-04 10:16 
GeneralRe: Error when I used NDoc, another question Pin
sreenaths@mailcity.com16-Dec-04 10:45
membersreenaths@mailcity.com16-Dec-04 10:45 
GeneralIs DLL missing a dependency on Build NDoc Pin
Anonymous2-Sep-04 1:51
sussAnonymous2-Sep-04 1:51 
QuestionHow can we enforce code comment Pin
Anonymous14-Jul-04 2:12
sussAnonymous14-Jul-04 2:12 
AnswerRe: How can we enforce code comment Pin
Ahmet Kara18-Aug-04 23:53
memberAhmet Kara18-Aug-04 23:53 
GeneralRe: How can we enforce code comment Pin
Patrick Long21-Aug-04 9:16
memberPatrick Long21-Aug-04 9:16 
Generalxml commenting Pin
chandler sue27-May-04 7:00
memberchandler sue27-May-04 7:00 
GeneralRe: xml commenting Pin
Patrick Long27-May-04 21:59
memberPatrick Long27-May-04 21:59 
GeneralRe: xml commenting Pin
Hermit Crab27-Jul-07 14:29
memberHermit Crab27-Jul-07 14:29 
GeneralXML samples within &lt;example&gt;&lt;code&gt; tags Pin
salb14-Apr-04 1:45
membersalb14-Apr-04 1:45 
GeneralRe: XML samples within &lt;example&gt;&lt;code&gt; tags Pin
Mathew Hall18-Jan-05 14:02
memberMathew Hall18-Jan-05 14:02 
Questionhow to document namespaces Pin
yzs6-Mar-04 23:02
memberyzs6-Mar-04 23:02 
AnswerRe: how to document namespaces Pin
EK_Kiwi6-Mar-04 23:37
memberEK_Kiwi6-Mar-04 23:37 
AnswerRe: how to document namespaces Pin
Jonathan de Halleux7-Mar-04 0:50
memberJonathan de Halleux7-Mar-04 0:50 
AnswerRe: how to document namespaces Pin
Mark Focas23-Mar-04 14:45
memberMark Focas23-Mar-04 14:45 
AnswerRe: how to document namespaces Pin
Anonymous2-Sep-04 1:39
sussAnonymous2-Sep-04 1:39 
GeneralNdoc *rocks* Pin
Jon Sagara20-Dec-03 15:53
memberJon Sagara20-Dec-03 15:53 
GeneralRe: Ndoc *rocks* Pin
WillemM23-Feb-04 5:55
memberWillemM23-Feb-04 5:55 
Generalcode element Pin
Mark Focas23-Oct-03 14:17
memberMark Focas23-Oct-03 14:17 
GeneralNDoc, overloads, and F1 Help Pin
Marilyn Kotwal23-Oct-03 13:02
memberMarilyn Kotwal23-Oct-03 13:02 
GeneralComment doesn't show in Intellisense Pin
Anydasdasd22-Oct-03 14:08
sussAnydasdasd22-Oct-03 14:08 
GeneralRe: Comment doesn't show in Intellisense Pin
Patrick Long23-Oct-03 22:42
memberPatrick Long23-Oct-03 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 21 Jan 2003
Article Copyright 2002 by Patrick Long
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid