Skip to content

Commit 299cc19

Browse files
CopilotBillWagner
andauthored
Enhance XML documentation for href attribute and br tag usage (#47049)
* Initial plan * Enhance XML documentation for href attribute and br tag usage Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> * Update URLs to use learn.microsoft.com domain Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
1 parent bdd935e commit 299cc19

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) \*\*
@@ -246,6 +249,10 @@ The `<value>` tag lets you describe the value that a property represents. When y
246249

247250
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.
248251

252+
Here's an example showing the difference between `<para>` and `<br/>`:
253+
254+
:::code language="csharp" source="./snippets/xmldoc/HrefAndBrExamples.cs" id="FormattingExample":::
255+
249256
### \<list>
250257

251258
```xml
@@ -416,11 +423,15 @@ The XML output for this method is shown in the following example:
416423
```
417424

418425
- `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.
419-
- `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`.
426+
- `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 external URLs.
420427
- `langword="keyword"`: A language keyword, such as `true` or one of the other valid [keywords](../keywords/index.md).
421428

422429
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.
423430

431+
Here's an example showing the difference between `cref` and `href` when referencing external URLs:
432+
433+
:::code language="csharp" source="./snippets/xmldoc/HrefAndBrExamples.cs" id="UrlLinkingExample":::
434+
424435
### \<seealso>
425436

426437
```xml
@@ -440,7 +451,7 @@ The `cref` attribute in an XML documentation tag means "code reference." It spec
440451

441452
### href attribute
442453

443-
The `href` attribute means a reference to a web page. You can use it to directly reference online documentation about your API or library.
454+
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.
444455

445456
## Generic types and methods
446457

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://learn.microsoft.com/dotnet/csharp"/> (won't create clickable link)
14+
/// <see href="https://learn.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)