How to Link to a Type from an XML Documentation Comment with see cref in C#
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
/// <see cref="<TypeName>"/>
/// <see cref="<TypeName>"><linkText></see>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
| Part | What 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 --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n SeeCref
cd SeeCref
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:
<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:
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:
/// <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:
/// <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:
/// <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:
dotnet run
The sky is sunny.
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

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()}.";
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
</Project>
See the Links in the XML File
The build wrote the XML file next to the program. Print it:
cat bin/Debug/net10.0/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
| Mistake | What happens | Do this instead |
|---|---|---|
Misspelling the type name in cref | cref="Skyy" gave warning CS1574: XML comment has cref attribute 'Skyy' that could not be resolved, and the program still built and ran | Fix the name. The CS1574 reference: Verify that the member or type name is spelled correctly |
Leaving out GenerateDocumentationFile | With the same cref="Skyy" and no GenerateDocumentationFile, the build gave no warning at all | Set GenerateDocumentationFile to true in the project file |
| Writing a generic type with angle brackets | cref="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 #57101 | Use 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 cref | cref="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.
Can the link show its own text?
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
- Recommended XML tags for C# documentation comments — the
seetag and itscrefandhrefattributes. - Generate XML API documentation comments — the XML file, generic types in a
cref, and ID strings. - Compiler Warning CS1574 — a
crefthat the compiler can't resolve. - Compiler Warning CS1570 — badly formed XML in a documentation comment.
- Compiler Warning CS1584 — a
crefwith incorrect syntax.
