Rock Your Code: XML Documentation of Code for Microsoft .NET

Have you ever had issues trying to figure out what the code is doing that you were tasked to work on? Is there any documentation for it? Do you need questions answered, but the original developer isn’t working at the company anymore? Well, there is an easy answer for .NET projects… XML documentation and code comments! These days, I’m beginning to think that documentation of code in Microsoft .NET is becoming a lost art. This is a bad trend that I will try to improve with this article. If you use good coding standards, most of the documentation I will talk about is simple to generate!

An enhanced edition of this article can be found in my book, “Rock Your Code: Coding Standards for Microsoft .NET,” now available on Amazon. This updated version comprises 230 new pages, providing valuable insights to help you and your team produce top-quality code that is easily modifiable.

Secure your copy by visiting the following link: https://bit.ly/CodingStandards8

XML DOCUMENTATION-TWEET

I will acknowledge that most of us don’t like documenting code, but it’s vitally important for the developers using the code now and even more in the future. Recently I ran a poll on Twitter and as you can see, over 45% of the respondents either don’t use XML documentation or don’t know what it is.

In the past when I talked about this at conferences and in my articles, I used the .NET Framework as a good example of documentation. Sadly, I can’t do that anymore since even the teams at Microsoft are doing a poor job… so don’t feel bad if you aren’t either. The information that I will show is from my book titled Rock Your Code: Coding Standards for Microsoft .NET.

The purpose of XML documentation is to document the intent of the class and its fields, properties, methods, and events. The documentation for an assembly can be used by tools to create human-readable documentation such as web pages, help files, and PDF files. There are good reasons to use XML documentation.

  • The compiler combines the structure of the code with the text of the comments into a single XML document.
  • The compiler verifies that the comments match the API signatures for relevant tags.
  • Tools that process the XML documentation files can define XML elements and attributes specific to those tools.
  • Visual Studio uses this information to provide the help text in IntelliSense.

If you follow good coding standards, then your code should be easy to understand (self-documenting), but there is more that needs to be done.

Setting it Up

First, we must configure the project to export the XML file that Visual Studio can create. Go to the Properties of the project and navigate to the Build → Output tab. Make sure to check the checkbox and fill in the path and the name of the file. By default, Visual Studio does not make these settings for you.

XML DOCUMENTATION-XML FILE

One added benefit to configuring this is that the Visual Studio analyzers will produce an error if a type or member has not been documented. This file is what is used to create, using other tools, a website, help file, PDF, etc. for developers that will be coding against the assembly.

Section Tags

All types, methods, fields, events, delegates, etc. should be documented using XML tags. These tags will allow IntelliSense to provide useful details while using the types. Also, automatic documentation generation tooling relies on these tags. Section tags define the different sections within the documentation for the type.

Section Tags

Description

Location

<completionlist>

** I Cannot Find Any Documentation On This Tag. **

 

<example> 

Contains examples (code or text) related to a member or a type

Type or member

<exception> 

Lists the exceptions that a method or property can throw

Method, Event, or Property

<include>

Let’s you refer to comments in another file that describe the types and members in your source code.

 

<inheritdoc>

Inherit XML comments from base classes, interfaces, and similar methods. This eliminates unwanted copying and pasting of duplicate XML comments and automatically keeps XML comments synchronized.

I’m not a big fan of this since the actual documentation cannot be viewed when looking at the code.

 

<list>

Used to define the heading row of either a table or definition list. When defining a table, you only need to supply an entry for the term in the heading.

 

<overloads> 

Provides a summary for multiple overloads of a method

The first method in an overload list

<para>

The text of the paragraph.

 

<param> 

Describes the parameters of a method

Method

<permission>

Allows you to document the access of a member. The PermissionSet class lets you specify access to a member.

 

<remarks> 

Describes preconditions and other additional information.

Type or member

<returns> 

Describes the return value of a method.

Method

<seealso> 

Adds an entry to the See Also section

Type or member

<summary> 

Short description.

Type or member

<typeparam>

Used in the comment for a generic type or method declaration to describe a type parameter. Add a tag for each type parameter of the generic type or method.

 

<typeparamref>

Use this tag to enable consumers of the documentation file to format the word in some distinct way, for example in italics.

 

<value> 

Describes the type of data a property accepts and/or returns

Property

Inline Tags

Inline tags can be used within the section tags.

Inline Tags

Description

<see>

Creates a hyperlink to another member or type.

<paramref>

Gives you a way to indicate that a word in the code comments, for example in a <summary> or <remarks> block refers to a parameter. The XML file can be processed to format this word in some distinct way, such as with a bold or italic font.

Markup Tags

Markup tags are used to apply special formatting to a part of a section.

Markup Tags

Description

<code>

Changes the indentation policy for code examples

<c>

Changes the font to a fixed-wide font (often used with the <code> tag)

<para>

Creates a new paragraph

<list>

Creates a bulleted list, numbered list, or table.

<b>

Bold typeface

<i>

Italics typeface

File Headers

Each file should contain a header block. The header block should consist of the following which adheres to the format defined by StyleCop.

// ****************************************************************
// Assembly         : dotNetTips.Spargine.6.Extensions
// Author           : David McCarter
// Created          : 01-16-2022
//
// Last Modified By : David McCarter
// Last Modified On : 07-14-2022
// *******************************************************************
// <copyright file="ImmutableArrayExtensions.cs"
//     company="David McCarter - dotNetTips.com">
//     McCarter Consulting (David McCarter)
// </copyright>
// <summary>Extension methods for the ImmutableArray.</summary>

I don’t know of a way to automatically keep the “Last Modified By” and “Last Modified On” up to date, so make sure you change these values if you work on the file.

Types & Members

Minimally, XML documentation should be written for all public and protected classes and members. This documentation should be kept up to date with the code when changes are made. XML documentation of classes and members should at a minimum include the <summary> tag. You should strive to use any tag that is appropriate for the type or member. More information makes the intent for types and members easier to understand by other developers. Remember, these comments are for developers, not the end user.

Types

Here is an example of how to properly document a type.

/// <summary>
/// Class HttpEventListener. This class cannot be inherited.
/// Implements the <see cref="EventListener" />
/// </summary>
/// <example>
/// <b>Output:</b> HTTP RequestStart:
///                00000011-0000-0000-0000-00008ed19d59
///                https://dotnettips.com:443/
///                HTTP/1.1 POLICY: RequestVersionOrLower
/// <b>Output:</b> HTTP RequestStop:
///                00000011-0000-0000-0000-00008ed19d59
/// </example>
/// <seealso cref="EventListener" />
public sealed class HttpEventListener : EventListener

Members

When I document the code I write, I document all members. At a minimum, all public and protected members should be documented using the tags previously described. I will show and explain a few examples. All the following examples are from my Spargine open-source libraries.

/// <summary>
/// Logs default application information as key/ value pairs to
///   <see cref="ILogger"/>.
/// </summary>
/// <param name="logger">The ILogger.</param>
/// <exception cref="ArgumentNullException">Logger cannot be
///   null.</exception>
/// <example>
/// <b>Output:</b>
/// AppInfo:Company - Microsoft Corporation
/// AppInfo:Version - 16.8.0
/// AppInfo:Copyright - © Microsoft Corp. All rights reserved.
/// AppInfo:Product - dotNetTips.Spargine
/// AppInfo:FileVersion - 15.0.0
/// AppInfo:Title - dotNetTips.Spargine
/// </example>
public static void LogApplicationInformation(ILogger logger)

This example uses the <exception> tag to let the caller know what Exceptions are thrown from the method. This is very useful when coding try/catch blocks. It also uses the <example> tag to show the caller, in this case, an example of the result.

This should also be used to show how to properly use the method. Here is an example.

/// <summary>
/// Processes the <see cref="IEnumerable{T}" /> with the specified action in
/// parallel processing.
/// </summary>
/// <typeparam name="T">Generic type</typeparam>
/// <param name="collection">The source.</param>
/// <param name="action">The action to perform.</param>
/// <param name="maxDegreeOfParallelism">The maximum degree of parallelism.</param>
/// <param name="ensureOrdered">if set to <c>true</c> [ensure ordered].</param>
/// <param name="scheduler">The scheduler.</param>
/// <returns>Task.</returns>
/// <exception cref="ArgumentInvalidException">collection cannot be null.
/// </exception>
/// <exception cref="ArgumentInvalidException">action cannot be null.</exception>
/// <example>
/// <code>
/// var task = people.FastParallelProcessor((Person person) =>
///       {
///            person.Address2 = "TEST DATA";
///       }, App.MaxDegreeOfParallelism());
/// </code>
/// </example>
public static Task FastParallelProcessor<T>(this IEnumerable<T> collection,
    Action<T> action, int maxDegreeOfParallelism = 
    DataflowBlockOptions.Unbounded,
    bool ensureOrdered = false, TaskScheduler scheduler = null)

Code

XML comments are for comments within methods. If you follow proper coding standards, then most code should be self-documenting. If more documentation is needed, you can use // or ///. Here is an example.

protected SalesforceService Service
{
    get
    {
           lock (_lock)
           {
               // Determine if service needs to log in
               if (this._service.ServiceStatus == ServiceStatus.LoggedOut)
               {
                      this.InitService();
               }
           }

           return this._service;
    }
}

Again, code comments like this make it much easier for developers to fix bugs or add features in the future. The more information you give, the better it will be. This will dramatically speed up working on the code.

Indent comments at the same level of indentation as the code you are documenting. All comments should pass spell-checking. Misspelled comments indicate sloppy development. These comments will be removed when the assembly is compiled.

Example of the XML File

Below is a partial example of what the XML documentation file looks like. This file is used to generate human-readable documentation. Before there were tools to create documentation, I wrote code that would convert the XML file into a Word document. I supplied these documents to companies that hired me on a contract basis so that when I left, they would know how to use the assembly.

XML DOCUMENTATION-XML FILE EXAMPLE

Generate Comments with GhostDoc

GhostDoc is a free Visual Studio extension for developers who need to generate XML comments from source code using customizable templates. It also helps you maintain clean and up-to-date documentation, produce help documentation in multiple formats and uses an intelligent source code spell checker in Visual Studio, and more.

I have been using GhostDoc from Submain.com for a long time to make it super easy to document my classes. If you use proper naming standards, then GhostDoc will write most of the documentation for you! You might have to tweak it a bit, but over 90% will be done automatically.

Key Features

Here are some of the key features of GhostDoc.

  • Automatic XML Comment Generation
  • Customizable XMAL Comment Templates
  • Documentation from the Build.
  • Visual Comment Editor.
  • Documentation in your preferred format
  • Smart Spell Check
  • Easy Code Documentation Maintenance
  • Custom Help Content
  • StyleCop Compliance

I use GhostDoc Enterprise. There is also a Pro version along with a FREE Community version. The spell checker in GhostDoc works well to ensure the comments are spelled correctly, but also types, members, and parameters!

GhostDoc will also use the documentation from interfaces, if available. For example, this is the default documentation for the Dispose() method from the IDisposable interface.

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed
///   and unmanaged resources; <c>false</c> to release only
///   unmanaged resources.</param>
protected virtual void Dispose(bool disposing)

The feature I use the most with GhostDoc is “Document File”. This will generate a file header and XML documentation for all the classes and members in a file at one time. On top of that, it will update the “Last Modified On” date in the file header!

More Examples

Go here to see how I document my open-source libraries:  https://bit.ly/SpargineGitHub. For more about GhostDoc, you can view my 256 Seconds With dotNetDave – My Workflow Before I Submit Code Changes video by going to https://bit.ly/CheckInWorkFlow.

Summary

I hope that this article will inspire you and your team to write good XML documentation and code comments for your projects. Your fellow developers will find it easier to use the assemblies you write, and it will make it much easier to make changes in the future and a lot less costly! This and much more are documented in my book Rock Your Code: Coding Standards for Microsoft.NET which is available on Amazon.com.

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.