Skip to content

Commit 3561c10

Browse files
authored
Merge pull request #12484 from v-thepet/snippets3
Freshness Pass: VS Code Snippets (#3 of 3)
2 parents 9a08450 + 8f51f1f commit 3561c10

6 files changed

+186
-120
lines changed
Loading
3.5 KB
Loading
Loading
Lines changed: 110 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,123 @@
11
---
22
title: Insert XML documentation comments
3-
description: Learn how to insert XML documentation comments in your code that you can use to create a compiler-generated XML file to distribute alongside your .NET assembly.
4-
ms.date: 11/23/2021
5-
ms.topic: reference
3+
description: Learn how to insert XML documentation comments in your code and create a compiler-generated XML comments file to distribute with your .NET assembly.
4+
ms.date: 04/11/2024
5+
ms.topic: how-to
66
author: mikadumont
77
ms.author: midumont
88
manager: mijacobs
99
ms.subservice: general-ide
1010
---
1111
# Insert XML comments for documentation generation
1212

13-
Visual Studio can help you document code elements such as classes and methods, by automatically generating the standard XML documentation comment structure. At compile time, you can generate an XML file that contains the documentation comments. To enable that option, select **Generate a file containing API documentation** on the **Build** > **Output** tab of your project's properties.
13+
This article describes how Visual Studio can help you document code elements such as classes and methods by automatically generating the standard XML documentation comment structure. At compile time, you can generate an XML file that contains the documentation comments.
1414

15-
> [!TIP]
16-
> If you want to configure a non-default name and location for the documentation file, add the [DocumentationFile](/dotnet/core/project-sdk/msbuild-props#documentationfile) property to your *.csproj*, *.vbproj*, or *.fsproj* file.
17-
18-
The compiler-generated XML file can be distributed alongside your .NET assembly so that Visual Studio and other IDEs can use IntelliSense to show quick information about types and members. Additionally, the XML file can be run through tools like [DocFX](https://dotnet.github.io/docfx/) and [Sandcastle](https://www.microsoft.com/download/details.aspx?id=10526) to generate API reference websites.
15+
You can distribute the compiler-generated XML file along with your .NET assembly so that Visual Studio and other IDEs can use IntelliSense to show quick information about types and members. You can also run the XML file through tools like [DocFX](https://dotnet.github.io/docfx/) and [Sandcastle](https://www.microsoft.com/download/details.aspx?id=10526) to generate API reference websites.
1916

2017
> [!NOTE]
21-
> The **Insert Comment** command that automatically inserts XML documentation comments is available in [C#](/dotnet/csharp/programming-guide/xmldoc/) and [Visual Basic](/dotnet/visual-basic/programming-guide/program-structure/how-to-create-xml-documentation). However, you can manually insert [XML documentation comments in C++](/cpp/build/reference/xml-documentation-visual-cpp) files and still generate XML documentation files at compile time.
22-
23-
## To insert XML comments for a code element
24-
25-
1. Place your text cursor above the element you want to document, for example, a method.
26-
27-
2. Do one of the following:
28-
29-
- Type `///` in C#, or `'''` in Visual Basic
30-
31-
- From the **Edit** menu, choose **IntelliSense** > **Insert Comment**
32-
33-
- From the right-click or context menu on or just above the code element, choose **Snippet** > **Insert Comment**
34-
35-
The XML template is immediately generated above the code element. For example, when commenting a method, it generates the **\<summary\>** element, a **\<param\>** element for each parameter, and a **\<returns\>** element to document the return value.
36-
37-
![XML comment template - C#](media/doc-preview-cs.png)
38-
39-
![XML comment template - Visual Basic](media/doc-preview-vb.png)
40-
41-
3. Enter descriptions for each XML element to fully document the code element.
42-
43-
![Screenshot showing the completed comment.](media/doc-result-cs.png)
44-
45-
You can use styles in XML comments that will render in Quick Info when hovering over the element. These styles include: italics, bold, bullets, and a clickable link.
46-
47-
![Screenshot showing the completed comment with style tags for italics, bold, bullets, and a clickable link.](media/doc-style-cs.png)
48-
49-
> [!NOTE]
50-
> There is an [option](../../ide/reference/options-text-editor-csharp-advanced.md) to toggle XML documentation comments after typing `///` in C# or `'''` Visual Basic. From the menu bar, choose **Tools** > **Options** to open the **Options** dialog box. Then, navigate to **Text Editor** > **C#** (or **Visual Basic**) > **Advanced**. In the **Editor Help** section, look for the **Generate XML documentation comments** option.
51-
52-
## See also
18+
> The **Insert Comment** command to automatically insert XML documentation comment structure is available in [C#](/dotnet/csharp/programming-guide/xmldoc/) and [Visual Basic](/dotnet/visual-basic/programming-guide/program-structure/how-to-create-xml-documentation). For C++, you can [manually insert XML documentation comments](/cpp/build/reference/xml-documentation-visual-cpp) and still generate XML documentation files at compile time.
5319
54-
- [Documenting your code with XML comments (C# Guide)](/dotnet/csharp/language-reference/xmldoc/)
55-
- [How to: Create XML documentation (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/how-to-create-xml-documentation)
56-
- [C++ Comments](/cpp/cpp/comments-cpp)
57-
- [XML Documentation (C++)](/cpp/build/reference/xml-documentation-visual-cpp)
58-
- [Code generation](../code-generation-in-visual-studio.md)
20+
## Enable documentation generation
21+
22+
::: moniker range=">= vs-2022"
23+
To enable documentation generation, select the **Generate a file containing API documentation** checkbox on the **Build** > **Output** tab of your project's properties.
24+
25+
By default, a documentation file named the same as your assembly with an *.xml* file extension generates in the same directory as the assembly. If you want to configure a nondefault name or location for the file, enter or browse to an alternate location under **XML documentation file path**.
26+
::: moniker-end
27+
28+
::: moniker range="<= vs-2019"
29+
To enable documentation generation, select the **XML documentation file** checkbox in the **Build** > **Output** section of your project's properties.
30+
31+
By default, a documentation file named the same as your assembly with an *.xml* file extension generates in the same directory as the assembly. If you want to configure a nondefault name or location for the file, enter or browse to an alternate location.
32+
::: moniker-end
33+
34+
Alternatively, you can add the [GenerateDocumentationFile](/dotnet/core/project-sdk/msbuild-props#generatedocumentationfile) or [DocumentationFile](/dotnet/core/project-sdk/msbuild-props#documentationfile) properties to your *.csproj*, *.vbproj*, or *.fsproj* file. Set `GenerateDocumentationFile` to `true` to generate a documentation file with the default name and location. Use the `DocumentationFile` property to specify a different name or location.
35+
36+
If you use `DocumentationFile` by itself or with the `GenerateDocumentationFile` property set to `true`, a documentation file with the specified name and location is generated. However, if you set `GenerateDocumentationFile` to `false`, no documentation file is generated even if you set the `DocumentationFile` property.
37+
38+
## Enable comment insertion keyboard shortcut
39+
40+
You can set the [Comments](options-text-editor-csharp-advanced.md#comments) option to automatically insert XML comment structures after you type `///` in C# or `'''` in Visual Basic.
41+
42+
1. From the Visual Studio menu bar, choose **Tools** > **Options**.
43+
1. In the **Options** dialog box, navigate to **Text Editor** > **C#** (or **Visual Basic**) > **Advanced**.
44+
1. Under the **Comments** section, select or deselect **Generate XML documentation comments for \\\\\\** (or **'''**).
45+
46+
## Automatically insert an XML comment
47+
48+
1. In Visual Studio, place your cursor above the element you want to document, for example a method.
49+
50+
1. Take one of the following actions:
51+
52+
- If the automatic comment insertion shortcut is enabled, type `///` in C#, or `'''` in Visual Basic.
53+
- From the **Edit** menu, choose **IntelliSense** > **Insert Comment**.
54+
- From the right-click or context menu, choose **Snippet** > **Insert Comment**.
55+
56+
The XML comment structure is immediately generated above the code element. For example, when commenting the following `GetUserName` method, the template generates the `<summary>` element, a `<param>` element for the parameter, and a `<returns>` element to document the return value.
57+
58+
```csharp
59+
/// <summary>
60+
///
61+
/// </summary>
62+
/// <param name="id"></param>
63+
/// <returns></returns>
64+
public string GetUserName(int id)
65+
{
66+
return "username";
67+
}
68+
```
69+
70+
```vb
71+
''' <summary>
72+
'''
73+
''' </summary>
74+
''' <param name="id"></param>
75+
''' <returns></returns>
76+
Public Function GetUserName(id As Integer) As String
77+
Return "username"
78+
End Function
79+
```
80+
81+
1. Enter descriptions for each XML element to fully document the code. For example:
82+
83+
```csharp
84+
/// <summary>
85+
/// Gets the username associated with the specified ID.
86+
/// </summary>
87+
/// <param name="id">The unique user ID.</param>
88+
/// <returns>A string containing the username for the specified ID.</returns>
89+
public string GetUserName(int id)
90+
{
91+
return "username";
92+
}
93+
```
94+
You can use XML elements and styles in comments that render in Quick Info when you hover over the code. These elements include italic or bold styles, bulleted or numbered lists, and clickable `cref` or `href` links.
95+
96+
For example, enter the following code into a C# program file:
97+
98+
```csharp
99+
/// <summary>
100+
/// There are two <see href="https://bing.com">params</see>.
101+
/// <list type="number">
102+
/// <item><param name="id">The user <em>id</em></param></item>
103+
/// <item><param name="username">The user <em>name</em></param></item>
104+
/// </list>
105+
/// </summary>
106+
/// <returns>The <strong>username</strong>.</returns>
107+
public static string GetUserName(int id)
108+
{
109+
return "username";
110+
}
111+
```
112+
113+
When you hover over **GetUserName**, the Quick Info pane appears as follows:
114+
115+
![Screenshot showing the completed comment with style tags for a clickable link, a numbered list, and italic and bold formatting.](media/doc-style-cs.png)
116+
117+
## Related content
118+
119+
- [Documentation comments](/dotnet/csharp/language-reference/xmldoc/)
120+
- [XML documentation in Visual Basic](/dotnet/visual-basic/programming-guide/program-structure/how-to-create-xml-documentation)
121+
- [Comments (C++)](/cpp/cpp/comments-cpp)
122+
- [XML Documentation (Visual C++)](/cpp/build/reference/xml-documentation-visual-cpp)
123+
- [Code generation](../writing-code-in-the-code-and-text-editor.md#generate-fix-or-refactor-code)
28.8 KB
Loading

0 commit comments

Comments
 (0)