Click here to Skip to main content
15,867,568 members
Articles / Web Development / HTML
Article

Documentation in C#

Rate me:
Please Sign up or sign in to vote.
3.05/5 (29 votes)
27 Oct 20068 min read 136K   33   20
This article explains about C# documentation and ways to automate it

Introduction

Documentation has been always a neglected task in the software development life cycle. While developing applications, most of the software developing professionals including senior professionals tend to forget about documentation although everyone is aware about the difficulties that are faced during code maintenance application. Documentation is a very important aspect specially when developing large and very big applications involving a number of modules.

When developing an application with C#, we can minimize the effort of making and maintaining the application documentation. As a C# application developer, it is common to give inline comments, these inline comments can be the source to generate a strong and more accurate documentation. Inline comments are the most important part of any code as they provide valuable information about the part of the code that is written. And most importantly it is written when the system is constructed. One of the wonderful features of the C# compiler is that it supports automatic creation of documentation using comments written in the code, the only distinction being that the documentation is produced in XML. Unlike other automated documentation provided by various programming languages, that generates HTML based documentation, C# produces in XML but there are tools available that take XML files as the input and produce HTML based documentation. As the C# stores all the comments in an XML file, users have the flexibility to develop excellent documentation from C#. Code comments, user manuals, developer manuals, test plans, and many other documents can be generated from a single source using XML tags.

The process of generating an XML based documentation is done by adding comments to various parts of the code. The process can be described in the following manner:

Source file  (.cs) -> C# Compiler (csc.exe) -> XML Documentation (doc.xml) 
                -> HTML/ Visual Studio Based Documentation

In-depth of C# Documentation

Before we proceed in depth about the automated documentation part, let's see some of the important aspects that are required to be known when developing the XML based documentation. The main point is that the comments needs to start with "///" as this is the way in which the compiler identifies tags for XML based documentation. "//" way of comments are also supported in C# but in general, the compiler ignores anything that is put after this, but when the compiler encounters another "/" then this indicates that this is an XML comment and needs to be handled properly. Usually the three slashes are followed by the XML tag, this contains the property and value along with it as needed.
For example:

XML
/// <Summary>
/// this is a place where something is done
/// </Summary>

There are various predefined tags that are available for the purpose of generating the document. The developer has the freedom to use any type of XML tags to document the code, only thing is that these tags need to adhere to the XML syntax. The predefined tag indicates the type of documentary information that is being given, and the compiler validates these tags against certain aspects of these and produces the output. Other elements are used for the layout and/or formatting purpose. The C# compiler takes these comments embedded inside the XML tags and builds the XML based documentation. It generates errors when a false reference is encountered.

The following lists describe the main documentation elements provided and are available for usage. The important thing to remember is that each element should be written between its opening and closing tags, Also, some of the tags take some further mandatory attributes. For example, the 'cref' attribute can be used in any element, but it should be used only when it is advisable.

TagDescription
<summary>This tag is used to hold the overview information of any documenting element.
<remarks> This helps to expand the comments that are given in the summary and usually follows it.
<param name="name"> Describes a parameter passed to a method. The compiler checks that the 'name' value matches an actual parameter in the code. Also, if you give documentation for one parameter value, the compiler will expect you to give documentation for them all.
<paramref name="name"> Identifies the mention of a parameter name within some other descriptive element, for instance within 'summary' tags. The idea is that this mentioned name can be styled differently from the surrounding text. The compiler checks that the 'name' value matches an actual parameter in the code.
<param name="name"> Describes a parameter passed to a method. The compiler checks that the 'name' value matches an actual parameter in the code. Also, if you give documentation for one parameter value, the compiler will expect you to give documentation for them all.
<returns> Describes the return value for a method. As the descriptive field is just free text there is no compiler checking.
<exceptions cref="type">Describes an exception that may be thrown by a method. The 'cref' attribute refers to a type or field (such as System.Exception), and the compiler checks that this reference is available from the current compilation environment.
<permission cref="type">Describes a permission requirement for a type or member. The cref attribute refers to a type or field (such as System.Security.PermissionSet), and the compiler checks that this reference is available from the current compilation environment.
<value> Describes a class property.
<example> Gives an example of the use of the referenced object (for example a class method). The 'example' element is often used with the following two elements.
<c> Marks up a single phrase or line as a code example. Generally used in conjunction with the 'example' element.
<code> Marks up multiple lines as code examples. Generally used in conjunction with the 'example' element.
<see cref ="type"> Used to identify a crossreference in the documentation; designed to be used inline within a description element. The cref attribute refers to a type or field, and the compiler checks that this reference is available from the current compilation environment. and the see also tag is used in a separate section. This allows the documentation to create crossreferences.
<seealso cref ="type"> Used to identify a crossreference in the documentation; different from 'see' in that it is designed to stand alone. The cref attribute refers to a type or field, and the compiler checks that this reference is available from the current compilation environment.

Some of the formatting tags are as follows:

TagDescription
<para> Used within descriptive tags like 'remarks', 'summary', etc. to wrap a single paragraph.
<list type = "bullet" | "number" | "table"> "Top level tags for a list, where this may be one of the three types shown.There are more elements associated with the list tag: the following code gives an example of these.
XML
<list type=""table"">
<listheader>
<term>Employee</term>
<description>Employee Type</description>
</listheader>
<item>
<term>XXXX</term>
<description>Administrator</description>
</item>
<item>
<term>YYYY</term>
<description>User</description>
</item>
</list>"

C# Documentation Example

Let's take an example and then try to use the above information to get an automated documentation file.

The above is a typical example of an application during the construction phase and then we struggle at the time of maintenance as to what the function or the class was supposed to do! Let's give some comments and do the needful.

C#
using System; 
using System.Data; 

namespace ExampleDocumentation
{ 
/// <summary>
///  DocumentationClass: Is the class that will be used for explaining the 
///  example of automated documentation
/// </summary> 

    public class DocumentationClass
    { 
    /// <summary>
    /// public DocumentationClass () : 
    ///     Is the constructor used for DocumentationClass class
    /// </summary> 

    public DocumentationClass () 
    { 
    } 

    /// <summary>
    /// public void DocumentationClassMethod: Is a void method 
    /// that does not return anything and takes no parameters.
    /// It does the functionality of some addition and subtraction to 
    /// get the result
    /// </summary> 
    public void DocumentationClassMethod () 
    { 
    } 
   } 
}

As you can see above, now we have added some valuable comments that tend to have lots of value. Now to generate the documentation we can have the following command

XML
<Directory>\Microsoft.NET\Framework\v1.1.4322>csc csc 
                        DocumentationClass.cs /doc:doc.xml

In the above, command /doc instructs the compiler to produce the documentation named doc.xml. And this will produce the following output:

XML
<?xml version="1.0"?>
<doc>
    <assembly>
        <name> DocumentationClass</name>
    </assembly>
    <members>
        <member name="T:ExampleDocumentation.DocumentationClass">
            <summary>
             DocumentationClass: Is the class that will be used 
             for explaining the example of automated documentation
            </summary> 
        </member>
        <member name="M:ExampleDocumentation.DocumentationClass.#ctor">
            <summary>
            public DocumentationClass () : 
            Is the constructor used for DocumentationClass class
            </summary> 
        </member>
        <member name=
    "M:ExampleDocumentation.DocumentationClass.DocumentationClassMethod">
            <summary>
            public void DocumentationClassMethod: 
            Is a void method that does not return anything 
    /// and takes no parameters. 
    ///It does the functionality of some addition and substation to 
            get the result
            </summary> 
        </member>
    </members>
</doc>

Some points

Some of the constants are attached usually with the commented object, these are the ID's that are used to identify the type code and part. Usually these are categorised in the following manner:

  • N denotes the Namespace
  • T denotes Type, and includes class, Interface, struct, enum or delegates
  • F denotes the fields of class
  • P denotes the Property indexer or indexed property
  • M denotes the Methods special methods constructor and operator(overloaded)
  • E denotes Events
  • ! denotes an error string that C# was unable to resolve some references

Generation of C# documentation with Visual Studio NET 2000 IDE

The documentation can be generated in the following way from the Visual Studio NET 2000 IDE:

  • Open the property page for the project, usually by right-clicking on the project in the Solution Explorer, and click Properties.
  • Click the Configuration Properties folder.
  • Click the Build option.
  • In the right pane, there will be a property field called XML Documentation File. Enter the required information like the path and name of the file.

Generating HTML based Documentation with Visual Studio .NET

The XML based on the C# compiler can be used to generate the HTML based documentation. Visual Studio takes the doc.xml file generated by the C# compiler and produces whole documentation of HTML pages. To do this we need to follow the same process and then go to the Tools menu in Visual Studio .NET and select Build Comment Web Pages, there will be a dialog box that will appear. In this, select the projects to create the documentation. There is also the possibility to choose the specific part of the solution to generate the documentation.

Problems with the C# Documenter

The automated documentation in C# has some flaws; following are some of them that need to be taken into consideration:

  1. If in any case if you have forgotten to mention any comments for any method, it will just be ignored
  2. Even if the comments are written and you have written "//" instead of "///" then again the documentation will not have any reference of these comments
  3. While generating C# documentation whenever there is any kind of return type explicitly this has to be specified

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


Written By
Software Developer (Senior)
India India
Professionally I am a Software Engineer. Have over 6 years of experience and specialize in Microsoft Tech. I have done most of my project in VB, ASP, ASP.NET (1.1 and 2.0), C# (1.1 and 2.0) with back end support of MS-Access, Oracle, MS - SQL server etc. Also worked as the DBA for MS SQL Server (2000, 2005 and 2008).

I have stamp of Brain Bench and Microsoft certified professional to enhance my skill set.

For any query, reach me at jaiprakash.bankoll@gmail.com. Will be very happy to respond back.

I am a brain bench certified professional view my brainbench transcript click here

PS: Am also know with the nick name of OracleQuest

Please write your comments or suggestion in my blog

Comments and Discussions

 
GeneralTypo in your example Pin
clefranc23-Sep-14 16:34
clefranc23-Sep-14 16:34 
GeneralRe: Typo in your example Pin
Jaiprakash M Bankolli7-Dec-14 17:20
Jaiprakash M Bankolli7-Dec-14 17:20 
GeneralCommercial product option for C# .NET Documentation Pin
Richard Slogget4-Oct-12 12:58
Richard Slogget4-Oct-12 12:58 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey15-Feb-12 23:39
professionalManoj Kumar Choubey15-Feb-12 23:39 
Generalany automatic documentation tool for c# Pin
karthick2211198816-Feb-10 3:28
karthick2211198816-Feb-10 3:28 
GeneralRe: any automatic documentation tool for c# Pin
Jaiprakash M Bankolli16-Feb-10 3:34
Jaiprakash M Bankolli16-Feb-10 3:34 
GeneralDocumentation too much hassle Pin
Rassler4824-Oct-09 2:03
Rassler4824-Oct-09 2:03 
GeneralRe: Documentation too much hassle Pin
Jaiprakash M Bankolli24-Oct-09 2:57
Jaiprakash M Bankolli24-Oct-09 2:57 
GeneralBuild Comment Web Pages Pin
rynchan18-Jun-07 17:52
rynchan18-Jun-07 17:52 
GeneralRe: Build Comment Web Pages Pin
Jaiprakash M Bankolli18-Jun-07 22:25
Jaiprakash M Bankolli18-Jun-07 22:25 
GeneralRe: Build Comment Web Pages Pin
eric_yu13-Apr-11 15:19
eric_yu13-Apr-11 15:19 
Base on your comments, where can I download automated tool that can take xml and generate either chm or html files?
GeneralRe: Build Comment Web Pages Pin
Jaiprakash M Bankolli28-Apr-11 19:49
Jaiprakash M Bankolli28-Apr-11 19:49 
GeneralPrepare your code Pin
Sceptic Mole5-Nov-06 2:52
Sceptic Mole5-Nov-06 2:52 
GeneralRe: Prepare your code Pin
Jaiprakash M Bankolli5-Nov-06 5:01
Jaiprakash M Bankolli5-Nov-06 5:01 
GeneralIE setting for HTML output Pin
MyDays9-Aug-06 11:00
MyDays9-Aug-06 11:00 
GeneralRe: IE setting for HTML output Pin
Jaiprakash M Bankolli10-Aug-06 22:32
Jaiprakash M Bankolli10-Aug-06 22:32 
QuestionDocumentation in C# - ENUM Pin
Steini kallinn12-Mar-06 2:18
Steini kallinn12-Mar-06 2:18 
AnswerRe: Documentation in C# - ENUM Pin
Jaiprakash M Bankolli21-Mar-06 5:22
Jaiprakash M Bankolli21-Mar-06 5:22 
GeneralRe: Documentation in C# - ENUM Pin
Michael Freidgeim25-Oct-06 16:03
Michael Freidgeim25-Oct-06 16:03 
GeneralRe: Documentation in C# - ENUM Pin
Jaiprakash M Bankolli25-Oct-06 21:26
Jaiprakash M Bankolli25-Oct-06 21:26 

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

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