|
1 | | -Code comments should end with a period. |
| 1 | +# .NET API Reference Docs — Copilot Instructions |
2 | 2 |
|
3 | | -When you add a code snippet to the XML remarks of an API, add the code as a separate code file (.cs file) and not as an inline (```) code block. Also add a .csproj file to compile the code if one doesn't already exist in the snippet folder. |
| 3 | +## Repository overview |
4 | 4 |
|
5 | | -Don't use the word "may". Use "might" to indicate possibility or "can" to indicate permission. |
| 5 | +This repo contains the official .NET API reference documentation in ECMAXML format. The `xml/` directory holds one XML file per type, organized by namespace (e.g., `xml/System/String.xml`). Documentation is published via DocFX and ECMA2Yaml to [learn.microsoft.com](https://learn.microsoft.com). |
6 | 6 |
|
7 | | -There should always be a comma before a clause that begins with "which". |
| 7 | +Some namespaces (notably `Microsoft.Extensions.*`, `System.CommandLine.*`, `System.Formats.*`, `System.Numerics.Tensors.*`) are auto-generated from source code in other repos. Their `open_to_public_contributors` metadata is set to `false` in `docfx.json`. Don't edit those XML files directly. |
8 | 8 |
|
9 | | -Use a conversational tone with contractions. |
| 9 | +## Build and validation |
10 | 10 |
|
11 | | -Be concise. |
| 11 | +- **Snippet compilation** — The CI workflow "Snippets 5000" compiles all code samples on PRs to `main`. To build a single snippet project locally: |
12 | 12 |
|
13 | | -Break up long sentences. |
| 13 | + ``` |
| 14 | + dotnet build snippets/csharp/System/String/Overview/Project.csproj |
| 15 | + ``` |
14 | 16 |
|
15 | | -Use the present tense for instructions and descriptions. For example, "The method returns a value" instead of "The method will return a value." |
| 17 | +- There's no repo-wide `dotnet build` or test command. Validation happens through the OPS (Open Publishing System) build triggered on PRs. |
16 | 18 |
|
17 | | -Use the Oxford comma in lists of three or more items. |
| 19 | +## ECMAXML format |
| 20 | + |
| 21 | +Each type file (`xml/<Namespace>/<TypeName>.xml`) follows this structure: |
| 22 | + |
| 23 | +```xml |
| 24 | +<Type Name="TypeName" FullName="Namespace.TypeName"> |
| 25 | + <TypeSignature Language="C#" Value="..." /> |
| 26 | + <AssemblyInfo>...</AssemblyInfo> |
| 27 | + <Docs> |
| 28 | + <summary>Brief description.</summary> |
| 29 | + <remarks> |
| 30 | + <format type="text/markdown"><![CDATA[ |
| 31 | +## Remarks |
| 32 | +Detailed markdown content here. |
| 33 | + ]]></format> |
| 34 | + </remarks> |
| 35 | + </Docs> |
| 36 | + <Members> |
| 37 | + <Member MemberName="MethodName"> |
| 38 | + <MemberSignature Language="C#" Value="..." /> |
| 39 | + <MemberType>Method</MemberType> |
| 40 | + <Docs> |
| 41 | + <param name="paramName">Description.</param> |
| 42 | + <summary>Brief description.</summary> |
| 43 | + <returns>Description.</returns> |
| 44 | + <remarks>...</remarks> |
| 45 | + <exception cref="T:System.ArgumentException">Description.</exception> |
| 46 | + </Docs> |
| 47 | + </Member> |
| 48 | + </Members> |
| 49 | +</Type> |
| 50 | +``` |
| 51 | + |
| 52 | +Key points: |
| 53 | + |
| 54 | +- Don't edit the `<TypeSignature>`, `<MemberSignature>`, `<AssemblyInfo>`, `<TypeForwardingChain>`, or `<FrameworkAlternate>` elements (or their attributes/values) — they are auto-generated. |
| 55 | +- Editable content lives inside `<Docs>` elements: `<summary>`, `<remarks>`, `<param>`, `<returns>`, `<exception>`, `<example>`, `<seealso>`, and `<altmember>`. |
| 56 | +- Longer content goes inside `<format type="text/markdown"><![CDATA[ ... ]]></format>` blocks, which support full Markdown. |
| 57 | +- Namespace-level docs are in files like `xml/ns-System.xml`. |
| 58 | + |
| 59 | +## Cross-references |
| 60 | + |
| 61 | +Use these patterns to link to other APIs: |
| 62 | + |
| 63 | +- `<see cref="T:System.String" />` — link to a type. |
| 64 | +- `<see cref="M:System.String.Clone" />` — link to a method. |
| 65 | +- `<see cref="P:System.String.Length" />` — link to a property. |
| 66 | +- `<paramref name="value" />` — reference a parameter by name. |
| 67 | +- `<altmember cref="T:System.Text.StringBuilder" />` — "see also" link. |
| 68 | +- Inside markdown (`<format>`) blocks, use `<xref:System.String>` for cross-references. |
| 69 | + |
| 70 | +DocId prefixes: `T:` (type), `M:` (method), `P:` (property), `F:` (field), `E:` (event), `N:` (namespace). |
| 71 | + |
| 72 | +Don't escape backticks or asterisks in xref DocIDs. Use literal `` ` `` and `*` characters, not URL-encoded forms like `%60` or `%2A`. For example, use `` <xref:System.Collections.Generic.List`1> ``, not `<xref:System.Collections.Generic.List%601>`. |
| 73 | + |
| 74 | +## Code snippets |
| 75 | + |
| 76 | +Snippets are standalone compilable projects in `snippets/{language}/{Namespace}/{Type}/`. |
| 77 | + |
| 78 | +### Referencing snippets from XML |
| 79 | + |
| 80 | +Inside a `<format>` CDATA block, reference snippets with: |
| 81 | + |
| 82 | +```markdown |
| 83 | +:::code language="csharp" source="~/snippets/csharp/System/String/Overview/example.cs" id="Snippet1"::: |
| 84 | +``` |
| 85 | + |
| 86 | +### Snippet file conventions |
| 87 | + |
| 88 | +- Mark regions in source files with `// <Snippet1>` and `// </Snippet1>` comment pairs. |
| 89 | +- Each snippet folder should have a `.csproj` file so the CI can compile it. Use SDK-style projects: |
| 90 | + |
| 91 | + ```xml |
| 92 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 93 | + <PropertyGroup> |
| 94 | + <OutputType>Library</OutputType> |
| 95 | + <TargetFramework>net10.0</TargetFramework> |
| 96 | + </PropertyGroup> |
| 97 | + </Project> |
| 98 | + ``` |
| 99 | + |
| 100 | +- Use `OutputType` of `Exe` if the snippet has a `Main` method or uses top-level statements, or `Library` otherwise. |
| 101 | +- Add NuGet `<PackageReference>` entries if the snippet uses APIs outside the base runtime. |
| 102 | +- Snippet languages: `csharp`, `fsharp`, `visualbasic`. Each goes in its respective `snippets/` subdirectory. |
| 103 | + |
| 104 | +### Reusable includes |
| 105 | + |
| 106 | +Shared markdown fragments live in `includes/` and are referenced from CDATA blocks as: |
| 107 | + |
| 108 | +```markdown |
| 109 | +[!INCLUDE[description](~/includes/filename.md)] |
| 110 | +``` |
| 111 | + |
| 112 | +## Writing style |
| 113 | + |
| 114 | +- Code comments should end with a period. |
| 115 | +- Don't use the word "may". Use "might" to indicate possibility or "can" to indicate permission. |
| 116 | +- Always put a comma before a clause that begins with "which". |
| 117 | +- Use a conversational tone with contractions. |
| 118 | +- Be concise. Break up long sentences. |
| 119 | +- Use the present tense. For example, "The method returns a value" instead of "The method will return a value." |
| 120 | +- Use the Oxford comma in lists of three or more items. |
0 commit comments