Welcome to MSDN Blogs Sign in | Join | Help
C# XML documentation comments FAQ

Ever since we released the first version of C# 1.0 I’ve received a question or two a month about XML documentation comments.  These are often referred to as ‘doc comments’ for short.  The questions range from the use of doc comments in VS to the recommended schema of the XML.  This post captures a few of the common questions that I’ve seen.

Why isn’t there a multi-line version of XML doc comments?

There are actually two forms of doc comments in C#, single and multi-line.  However, the single-line version is by far the most commonly used; in fact, the multi-line version wasn’t supported until the 1.1 version of the compiler even though they were defined in the 1.0 version of the language spec.  The single line version is likely to be familiar to any user of Visual Studio; it’s syntactically indicated on a line that starts with a triple slash (///):

        /// <summary>

        /// This is the single line version of the doc comment

        /// </summary>

        static void Example()

        {

        }

 

The multi-line version uses /**:

        /** <summary>

         * This is the multi-line version

         * </summary> */

        static void Example()

        {

        }

 

They are functionally identical, the only difference being that it’s possible to use the multi-line version “inline” within an expression.  The multi-line version of the comments weren’t actually in the proposed version of the language specification submitted to ECMA; however, the ECMA committee decided that having both forms would be better.

The C# language service doesn’t support multi-line XML doc comments as well as the singe line comments (i.e. /** doesn’t auto-generate any tags); however, colorization for multi-line doc comments does work, and in VS 2005 it’s possible to get completion lists for the tags but you must first end the multi-line comment and then go back and enter in the tags.

How do I make VS show the XML doc comments of the types and methods of my components in completion lists and object browser?

This has been an extremely common question for a long time.  The short and long of it is that you must deploy the XML file that is generated by the compiler with the component.  They must be in the same directory, and the XML file must have the same name as the component except with an .xml extension.  I wrote a whitepaper that contains a walkthrough, in VS 2003, which demonstrates this.  It’s available here.

How do use XML doc comments to refer to generic types?

Several of the tags that are recommended in the C# language specification have an attribute named ‘cref’ on them.  Cref refers to a code reference.  This attribute can be used by tools to create links between different types and members (e.g. object browser uses crefs to create hyperlinks that allow quick navigation to the related type).  The C# compiler actually understands the cref attribute to a limited extent.  The compiler will try to bind the type or member listed in a cref attribute to a code element defined your source.  Assuming that it can it will then fully qualify that member in the generated XML file.  For example:

using System.Collections;

 

class Program

{

    /// <summary>

    /// DoSomething takes a <see cref="ArrayList"/>

    /// </summary>

    void DoSomething(ArrayList al) { }

}

This generates the following XML file:

        <member name="M:Program.DoSomething(System.Collections.ArrayList)">

            <summary>

            DoSomething takes a <see cref="T:System.Collections.ArrayList"/>

            </summary>

        </member>

Notice that the compiler bound ArrayList and emitted System.Collections.ArrayList instead.

I’m sure you’re saying, wow, fascinating, um… but what does that have to do with generics?  Good question.  Generics complicate doc comments because C# uses angle brackets which would usually be associated with XML.  It’s possible just to use the normal escaping mechanisms associated with angle brackets (&gt; &lt) in XML.  Unfortunately this turns out to look fairly ugly:

using System.Collections.Generic;

 

class Program

{

    /// <summary>

    /// DoSomething takes a <see cref="List&lt;T>"/>

    /// </summary>

    void DoSomething(List<int> al) { }

}

This can become particularly onerous when the generic type has many type arguments.  The compiler team decided to improve this by allowing an alternate syntax to refer to generic types and methods in doc comments.  Specifically, instead of using the open and close angle-brackets it’s legal to use the open and close curly braces.  The example above would then become:

using System.Collections.Generic;

 

class Program

{

    /// <summary>

    /// DoSomething takes a <see cref="List{T}"/>

    /// </summary>

    void DoSomething(List<int> al) { }

}

 

The compiler understands this syntax and will correctly bind List{T} to System.Collections.Generic.List<T>. 

When the <example> tag is used and there are a number of generic types or methods in the example, an easier approach is to simply surround the example with a CDATA block.  That way there is no need to escape less-than signs.

Which doc comment tags are understood and used by Visual Studio?

There are a number of tags that Visual Studio uses to process or present information:

Tag name

Tools that make use of the tag

summary

Completion lists, quick info, class view, class designer, object browser, object test bench

param

Parameter help, quick info, object test bench, class designer, object browser

exception

Completion lists, quick info, object browser

include

C# Compiler

returns

Object browser

see/seealso

Object browser

 

The ‘metadata as source’ feature, which is invoked when goto definition is performed on a type or member that is defined in metadata, processes a number of the tags documented in the C# language specification and tries to provide a reasonable view.

How do I generate HTML or .chm documentation from the XML file?

The generated XML file actually doesn’t contain enough information to fully generate good reference documentation.  In fact, it was an explicit goal to make the XML file contain just enough information to map the comment back to the associated code element in metadata.  Regardless, there are a number of tools that take the assembly and the generated XML file and produce a very nice looking, easy to browse output.  NDoc has been a favorite of many developers that I’ve talked to for quite a while.  I believe that development on NDoc has slowed somewhat; another option is SandCastle.

Posted: Monday, September 11, 2006 5:35 PM by AnsonH

Comments

paraesthesia said:

# September 12, 2006 12:48 PM

Patrick said:

Creating a .CHM file or other type of documentation file from the XML comments is an important feature with a lot of demand around it (just look at the number of downloads nDoc gets). Having more verbose comments ala GhostDoc is also something that is highly useful.

Being able to keep the documentation and the code in the same document (the .cs file) makes it far more likely that the documentation will actually stay up to date.

The XML comments feature should be expanded to include images, samples, and other expository text.

ResponseI agree with your suggestions and we're tracking them on a potential features list for a future version of VS.
# September 12, 2006 3:09 PM

Nikita A Zeemin said:

Greate post, thanks a lot! I just translated it to Russian and want to publish it on famous Russian programmer's site/forum http://rsdn.ru/ ("Russian Software Developers Network"). Can you grant me your permission to this? Of course, your copyright will be saved with the translated version of the article.

ResponseSure! I've got no problem with any of these posts being translated into other languages. Thanks for taking the time to do so :)
# September 15, 2006 12:13 AM

Charlie Calvert's Community Blog said:

This week you can watch the first in a series of videos featuring members of the Microsoft C# team. A video of Raj Pai, the Group Program Manager for the C# team, leads off the series.
# September 15, 2006 1:24 AM

Jan Kucera said:

Unfortunately there is no way of reflection on the comments as far as I know...
# September 15, 2006 4:58 AM

nzeemin said:

So, here is Russian-translated version of the post: http://rsdn.ru/article/devtools/CSxmlcmnt.xml

ResponseAwesome! Thanks!

# September 19, 2006 12:56 AM

Chris said:

Has there been any consideration of providing a step in the compilation process that would inject documentation comments into an assembly, thus making it possible to discover documentation comments via reflection? This could easily be accomplished via attributes and it would be quite useful to tool developers. It would also make an assembly "self-documenting" without having to worry about toting around an additional *.xml file

ResponseGood question. Honestly the choice to split the documentation and the metadata/il was intentional. There are a few reasons. The first is that the documentation is really useful only when developing an application, which tends to be a comparitvely small amount of the time as compared to its usage in running the application. That wouldn't make much of a difference if it were free to make use of attributes; however, there is a cost both in the size of the assembly and the load time. So, we decided to split it out such that a fairly trimed down version could be deployed to end users with the documentation being deployed separately to developers.

I generally think this was the right choice; however, I definitely agree that we should have and promote and easy way to access doc comment information. It sounds like an interesting topic for a future blog.
# September 21, 2006 8:22 AM

Clayton Firth's Blog said:

I have been struggling with this issue for a few weeks now. How to I put crefs into my C# doc comments...

# October 4, 2006 7:50 PM

Charlie Calvert's Community Blog said:

Learn Videos and Presentations The LINQ Framework: What's New in the May CTP Anders: Chatting about LINQ

# October 9, 2006 8:54 PM

Joe said:

One of your examples has a comment stating 'DoSomething takes a <see cref="List{T}"/>'.

In fact DoSomething takes a List<int>, not just any List<T>.  Is there a way to specify this constraint in the XML comments?

# October 31, 2006 5:26 AM

Chris Moore said:

Yes! Yes! Yes!

I totally agree with the comment above by Chris (not me, the other Chris) that there should be an option to embed documentation comments into an assembly. I spent days writing a unified reflection model (an Assembly DOM, if you will) that provided the documentation comments for a given type, method, etc. I work a lot with reflection and many, many times I have needed this information.

How lovely it would be to write:

DocComments comments = typeof(MyType).DocComments.

!!!!

# November 2, 2006 6:37 PM

Warren Tang said:

Reference C#XMLdocumentationcommentsFAQ

http://blogs.msdn.com/ansonh/archive/2006/09/11/750...

# January 13, 2008 8:26 PM

Warren Tang said:

Reference C#XMLdocumentationcommentsFAQ

http://blogs.msdn.com/ansonh/archive/2006/09/11/750...

# January 13, 2008 8:27 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker