Skip to main content

How to Link to a Type from an XML Documentation Comment with see cref in C#

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

Inside an XML documentation comment, <see cref="Sky"/> links to the Sky type by its name. The compiler checks that the type exists, warns when it doesn't, and writes the link into the XML documentation file.

An XML documentation comment can link to a type with the see tag. The C# reference: The <see> tag lets you specify a link from within text. Its cref attribute names what the link points to: The cref attribute in an XML documentation tag means "code reference." It specifies that the inner text of the tag is a code element, such as a type, method, or property.

Unlike the rest of the comment's text, a cref is checked by the compiler. The reference: Attach the cref attribute to any tag to reference a code element. The compiler verifies that this code element exists. If the verification fails, the compiler issues a warning.

Syntax​

Syntax
/// <see cref="<TypeName>"/>
/// <see cref="<TypeName>"><linkText></see>
Syntax (project file)
<GenerateDocumentationFile>true</GenerateDocumentationFile>
PartWhat it is
///Starts a line of an XML documentation comment
<see ... />A link inside the text of another tag, such as <summary> or <returns>
cref="<TypeName>"What to link to. The compiler checks that it exists
<TypeName>The type's name as you'd write it in code at that spot, such as Sky or string
<see ...><linkText></see>The same link, with your own text to show in place of the type's name
<GenerateDocumentationFile>The project setting that writes the comments to an XML file. Without it, a wrong cref gave no warning

The reference adds how the name is looked up: The compiler respects any using directives when it looks for a type described in the cref attribute.

The Example​

A console app called SeeCref has an enum, Sky, and a static class, Weather, whose Describe method returns a sentence about a sky. The comments on Weather and Describe link to Sky, and the comment on what Describe returns links to string. The project writes its comments to an XML file, so the build checks every link, and the file shows what each link became.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n SeeCref
cd SeeCref
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/SeeCref/SeeCref.csproj:
Determining projects to restore...
Restored /Users/you/source/SeeCref/SeeCref.csproj (in 46 ms).
Restore succeeded.

dotnet new console -n SeeCref creates a folder named SeeCref with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or SeeCref.csproj in Visual Studio.

Step 2: Write the Code​

First open SeeCref.csproj and add the GenerateDocumentationFile line to its PropertyGroup:

SeeCref.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

</Project>

The XML documentation reference: You set either the GenerateDocumentationFile or DocumentationFile option. The compiler finds all comment fields with XML tags in the source code and creates an XML documentation file from those comments. Turn it on for see cref as well: without it, a misspelled type name in a cref built with no warning at all, as Common Mistakes shows.

Then replace everything in Program.cs. First the two calls:

Program.cs (the calls)
Console.WriteLine(Weather.Describe(Sky.Sunny));
Console.WriteLine(Weather.Describe(Sky.Rainy));

Weather.Describe returns a sentence about a kind of sky, and the two calls print one for sunny and one for rainy.

Then the enum:

Program.cs (the Sky enum)
/// <summary>What the sky looks like.</summary>
enum Sky { Sunny, Cloudy, Rainy }

Sky has three values. The /// line above it is an XML documentation comment, and its <summary> describes the type. This is the type the links below point to.

Then a link to it:

Program.cs (a link to Sky)
/// <summary>Describes the weather for each <see cref="Sky"/> value.</summary>
static class Weather
{

<see cref="Sky"/> puts a link to the Sky type in the middle of the summary's text. The C# reference says what the compiler does with it: The compiler checks that the given code element exists and passes member to the element name in the output XML.

Last, the method's comments:

Program.cs (the method's links)
/// <summary>Returns a sentence about one <see cref="Sky"/> value.</summary>
/// <param name="sky">The sky to describe.</param>
/// <returns>A <see cref="string"/> such as The sky is sunny.</returns>
public static string Describe(Sky sky) => $"The sky is {sky.ToString().ToLowerInvariant()}.";
}

The method's <summary> links to Sky again, and <returns> links to string, written the way you write the type in code. sky.ToString().ToLowerInvariant() turns a value such as Sunny into the text sunny.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
The sky is sunny.
The sky is rainy.

macOS-style terminal window titled Terminal — SeeCref showing dotnet run printing The sky is sunny. and The sky is rainy.

The program printed The sky is sunny. and The sky is rainy., and the build showed no warning, because every cref names a type that exists. The links aren't in the program's output: they're in the XML file that the build wrote, which you'll open after the complete files.

The Complete Files​

macOS-style code window titled Program.cs — SeeCref showing two calls to Weather.Describe, a Sky enum with a summary comment, and a static Weather class whose summary, method summary and returns comments link to Sky and string with see cref tags

Program.cs
Console.WriteLine(Weather.Describe(Sky.Sunny));
Console.WriteLine(Weather.Describe(Sky.Rainy));

/// <summary>What the sky looks like.</summary>
enum Sky { Sunny, Cloudy, Rainy }

/// <summary>Describes the weather for each <see cref="Sky"/> value.</summary>
static class Weather
{
/// <summary>Returns a sentence about one <see cref="Sky"/> value.</summary>
/// <param name="sky">The sky to describe.</param>
/// <returns>A <see cref="string"/> such as The sky is sunny.</returns>
public static string Describe(Sky sky) => $"The sky is {sky.ToString().ToLowerInvariant()}.";
}
SeeCref.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

</Project>

The build wrote the XML file next to the program. Print it:

Terminal
cat bin/Debug/net10.0/SeeCref.xml
Output (SeeCref.xml)
<?xml version="1.0"?>
<doc>
<assembly>
<name>SeeCref</name>
</assembly>
<members>
<member name="T:Sky">
<summary>What the sky looks like.</summary>
</member>
<member name="T:Weather">
<summary>Describes the weather for each <see cref="T:Sky"/> value.</summary>
</member>
<member name="M:Weather.Describe(Sky)">
<summary>Returns a sentence about one <see cref="T:Sky"/> value.</summary>
<param name="sky">The sky to describe.</param>
<returns>A <see cref="T:System.String"/> such as The sky is sunny.</returns>
</member>
</members>
</doc>

Each cref now holds an ID string instead of the name you typed: Sky became T:Sky, and string became T:System.String. The XML documentation reference explains the two parts. First, The string starts with a single character and a colon that identifies the kind of member. T is for a type: A type is a class, interface, struct, enum, or delegate. Then, The second part of the string is the fully qualified name of the item, starting at the root of the namespace. That's why the keyword string became System.String, while Sky, which this example declares outside any namespace, stays Sky.

The same ID strings name the documented members. M:Weather.Describe(Sky) is the Describe method, where M is for a method, and the reference adds: For properties and methods, the parameter list enclosed in parentheses follows.

Common Mistakes​

MistakeWhat happensDo this instead
Misspelling the type name in crefcref="Skyy" gave warning CS1574: XML comment has cref attribute 'Skyy' that could not be resolved, and the program still built and ranFix the name. The CS1574 reference: Verify that the member or type name is spelled correctly
Leaving out GenerateDocumentationFileWith the same cref="Skyy" and no GenerateDocumentationFile, the build gave no warning at allSet GenerateDocumentationFile to true in the project file
Writing a generic type with angle bracketscref="List<Sky>" gave four CS1570 warnings, such as warning CS1570: XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'Sky'.', because the < started a new XML tag. The CS1574 page lists angle brackets as a cause of CS1574; I reported that to the .NET docs as issue #57101Use braces: cref="List{T}" built with no warning. The XML documentation reference: As a special case, the compiler parses the braces as angle brackets
Putting a web address in crefcref="https://learn.microsoft.com/dotnet/csharp" gave warning CS1584: XML comment has syntactically incorrect cref attribute 'https://learn.microsoft.com/dotnet/csharp'Use href: <see href="https://learn.microsoft.com/dotnet/csharp">the C# docs</see> built with no warning. The C# reference: Use href instead of cref when linking to external web pages

FAQ​

What does !: mean in the XML file?​

It marks a cref that the compiler couldn't resolve. With cref="Skyy", the XML file held <see cref="!:Skyy"/> where a working link holds T:Sky. The XML documentation reference lists ! as an error string: The rest of the string provides information about the error. The C# compiler generates error information for links that can't be resolved.

Yes. Put the text between an opening and a closing see tag. The C# reference: You can provide different link text for a cref, by using a separate closing tag. With <see cref="Sky">kind of sky</see>, the build gave no warning, and the XML file held <see cref="T:Sky">kind of sky</see>: the link resolved, and the text stayed.

Conclusion​

Write <see cref="Sky"/> in a documentation comment to link to a type, using the name you'd write in code at that spot. Turn on GenerateDocumentationFile, and the compiler checks every link: a misspelled name gives CS1574, and each link that resolves becomes an ID string such as T:Sky in the XML file. Use braces for a generic type, such as List{T}, and href for a web page.

Additional Resources​