Pluralsight Logo
Author avatar

Peter Mbanguo

Author badge Author

XML Documentation Comments In C#

Peter Mbanguo

Author BadgeAuthor
  • Aug 9, 2018
  • 5 Min read
  • 141 Views
  • Aug 9, 2018
  • 5 Min read
  • 141 Views
C#

Introduction

Code documentation describes the intent of a software’s code. This includes the intent or description of a class and its members, the parameters and the expected result, or the return value. There are various syntaxes for comments in C#. One of these is XML Documentation comments, sometimes called XML comments, which starts with a triple slash - ///. XML comments can be utilized by the IntelliSense feature of Visual Studio and VS Code to show information about a type or member; this information comes from what you put in your code documentation. The .NET compiler has an option that reads the XML documentation comments and generate XML documentation from them. This XML documentation can be extracted to a separate XML file. XML documentation comments must immediately precede a user-defined type (such as class, interface, or delegate) or its member (such as field, property, method, or event).

XML Documentation Comments

Let's look at a simple class such as the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Program
{
 static void Main(string[] args)
 {
   Console.WriteLine("Hello World!");

   Console.ReadKey();
 }

 ///The left side operand for an arithmetic operation
 static int Operand1 { get; set; }

 ///The right side operand for an arithmetic operation
 static int Operand2 { get; set; }

 /// This method adds two integers
 static int Add(int operand1, int operand2)
 {
   return operand1 + operand2;
 }

 ///This method subtracts two integers
 static int Subtract(int operand1, int operand2)
 {
   return operand1 - operand2;
 }
}

We added XML comments to properties Operand1 and Operand2, and to methods Add and Subtract. When you call any of these properties or methods with XML comments, they appear with a text description.

There are recommended XML tags you can use with XML comments. There are the <remarks>, <summary>, and <returns> tags.

The Remarks Tag

The <remarks> tag specifies a general commentary or description for a type, such as a class or structure.

1
2
3
4
5
6
7
///<remarks>Holds user basic data like their names and address
class User
{
 string FirstName { get; set; }

 string LastName { get; set; }
}

The Summary Tag

The <summary> tag is used to add general commentary explaining a method or other class members. If <remarks> is for a general commentary for a class, <summary> does the same but for the members of the class.

1
2
3
4
5
///<summary>Subtract two integers and return the result</summary>
static int Subtract(int operand1, int operand2)
{
 return operand1 + operand2;
}

The Returns Tag

The <returns> tag documents the return value of a method. So we can update the previous code to add <returns> tag as follows:

1
2
3
4
5
6
///<summary>Subtract two integers and returns the result</summary>
///<returns>the difference between the two operands</returns>
static int Subtract(int operand1, int operand2)
{
 return operand1 + operand2;
}

End Result

Applying the tags will look like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
///<remarks>Starts the console application</remarks>
class Program
{
 static void Main(string[] args)
 {
   Console.WriteLine("Hello World!");

   Console.ReadKey();
 }

 ///The left side operand for an arithmetic operation
 static int Operand1 { get; set; }

 ///The right side operand for an arithmetic operation
 static int Operand2 { get; set; }

 ///<summary>Adds two integers and return the result</summary>
 ///<returns>the difference between the two operands</returns>
 static int Add(int operand1, int operand2)
 {
   return operand1 + operand2;
 }

 ///<summary>Subtract two integers and return the result</summary>
 ///<returns>the difference between the two operands</returns>
 static int Subtract(int operand1, int operand2)
 {
   return operand1 - operand2;
 }
}

Then we can get IntelliSense when we want to use any of the class members

xml-comments-c#.gif

Wrap Up

XML Documentation comments can be extremely useful in helping you, or other programmers who will use your code, to understand the code better. We looked at three recommended tags with a class containing members that utilised them. You saw how it provides IntelliSense information from these comments.

Use XML documentation as much as possible and give more meaning to your code. See the C# documentation for more recommended tags.

2