Skip to content

Commit 7489748

Browse files
CopilotBillWagner
andcommitted
Enhance XML documentation for href attribute and br tag usage
Co-authored-by: BillWagner <[email protected]>
1 parent cad1bcc commit 7489748

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

docs/csharp/language-reference/xmldoc/recommended-tags.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ Some of the recommended tags can be used on any language element. Others have mo
118118

119119
The compiler verifies the syntax of the elements followed by a single \* in the following list. Visual Studio provides IntelliSense for the tags verified by the compiler and all tags followed by \*\* in the following list. In addition to the tags listed here, the compiler and Visual Studio validate the `<b>`, `<i>`, `<u>`, `<br/>`, and `<a>` tags. The compiler also validates `<tt>`, which is deprecated HTML.
120120

121+
> [!NOTE]
122+
> HTML tags like `<br/>` are useful for formatting within documentation comments. The `<br/>` tag creates line breaks, while other HTML tags provide text formatting. These tags work in IntelliSense tooltips and generated documentation.
123+
121124
- [General Tags](#general-tags) used for multiple elements - These tags are the minimum set for any API.
122125
- [`<summary>`](#summary): The value of this element is displayed in IntelliSense in Visual Studio.
123126
- [`<remarks>`](#remarks) \*\*
@@ -241,6 +244,10 @@ The `<value>` tag lets you describe the value that a property represents. When y
241244

242245
The `<para>` tag is for use inside a tag, such as [\<summary>](#summary), [\<remarks>](#remarks), or [\<returns>](#returns), and lets you add structure to the text. The `<para>` tag creates a double spaced paragraph. Use the `<br/>` tag if you want a single spaced paragraph.
243246

247+
Here's an example showing the difference between `<para>` and `<br/>`:
248+
249+
:::code language="csharp" source="./snippets/xmldoc/HrefAndBrExamples.cs" id="FormattingExample":::
250+
244251
### \<list>
245252

246253
```xml
@@ -368,11 +375,15 @@ The XML output for this method is shown in the following example:
368375
```
369376

370377
- `cref="member"`: A reference to a member or field that is available to be called from the current compilation environment. The compiler checks that the given code element exists and passes `member` to the element name in the output XML. Place *member* within quotation marks ("). You can provide different link text for a "cref", by using a separate closing tag.
371-
- `href="link"`: A clickable link to a given URL. For example, `<see href="https://github.com">GitHub</see>` produces a clickable link with text :::no-loc text="GitHub"::: that links to `https://github.com`.
378+
- `href="link"`: A clickable link to a given URL. For example, `<see href="https://github.com">GitHub</see>` produces a clickable link with text :::no-loc text="GitHub"::: that links to `https://github.com`. Use `href` instead of `cref` when linking to external web pages, as `cref` is designed for code references and won't create clickable links for URLs.
372379
- `langword="keyword"`: A language keyword, such as `true` or one of the other valid [keywords](../keywords/index.md).
373380

374381
The `<see>` tag lets you specify a link from within text. Use [\<seealso>](#seealso) to indicate that text should be placed in a See Also section. Use the [cref attribute](#cref-attribute) to create internal hyperlinks to documentation pages for code elements. You include the type parameters to specify a reference to a generic type or method, such as `cref="IDictionary{T, U}"`. Also, ``href`` is a valid attribute that functions as a hyperlink.
375382

383+
Here's an example showing the difference between `cref` and `href` when referencing external URLs:
384+
385+
:::code language="csharp" source="./snippets/xmldoc/HrefAndBrExamples.cs" id="UrlLinkingExample":::
386+
376387
### \<seealso>
377388

378389
```xml
@@ -392,7 +403,7 @@ The `cref` attribute in an XML documentation tag means "code reference." It spec
392403

393404
### href attribute
394405

395-
The `href` attribute means a reference to a web page. You can use it to directly reference online documentation about your API or library.
406+
The `href` attribute means a reference to a web page. You can use it to directly reference online documentation about your API or library. When you need to link to external URLs in your documentation comments, use `href` instead of `cref` to ensure the links are clickable in IntelliSense tooltips and generated documentation.
396407

397408
## Generic types and methods
398409

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
3+
namespace XmlDocumentationExamples
4+
{
5+
/// <summary>
6+
/// This class demonstrates the use of href attribute and br tag in XML documentation.
7+
/// </summary>
8+
public class HrefAndBrExamples
9+
{
10+
//<UrlLinkingExample>
11+
/// <summary>
12+
/// This method demonstrates URL linking:
13+
/// <see cref="https://docs.microsoft.com/dotnet/csharp"/> (won't create clickable link)
14+
/// <see href="https://docs.microsoft.com/dotnet/csharp">C# documentation</see> (creates clickable link)
15+
/// </summary>
16+
public void UrlLinkingExample()
17+
{
18+
// This method demonstrates the difference between cref and href for URLs
19+
}
20+
//</UrlLinkingExample>
21+
22+
//<FormattingExample>
23+
/// <summary>
24+
/// Example using para tags:
25+
/// <para>This is the first paragraph.</para>
26+
/// <para>This is the second paragraph with double spacing.</para>
27+
///
28+
/// Example using br tags:
29+
/// First line of text<br/>
30+
/// Second line of text with single spacing<br/>
31+
/// Third line of text
32+
/// </summary>
33+
public void FormattingExample()
34+
{
35+
// This method demonstrates paragraph and line break formatting
36+
}
37+
//</FormattingExample>
38+
39+
/// <summary>
40+
/// Comprehensive example showing different link types:
41+
/// <see cref="Console.WriteLine(string)">Console.WriteLine</see> (code reference with custom text)<br/>
42+
/// <see href="https://github.com/dotnet/docs">GitHub repository</see> (external link)<br/>
43+
/// <see langword="null"/> (language keyword)<br/>
44+
/// <see cref="string"/> (type reference)
45+
/// </summary>
46+
/// <remarks>
47+
/// This example shows:
48+
/// <list type="bullet">
49+
/// <item>Using cref for code elements</item>
50+
/// <item>Using href for external URLs</item>
51+
/// <item>Using langword for language keywords</item>
52+
/// <item>Using br for line breaks in summaries</item>
53+
/// </list>
54+
/// </remarks>
55+
public void ComprehensiveExample()
56+
{
57+
// This method demonstrates various linking and formatting options
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)