csharp-docs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseC# Documentation Best Practices
C# 文档编写最佳实践
- Public members should be documented with XML comments.
- It is encouraged to document internal members as well, especially if they are complex or not self-explanatory.
- 公共成员应使用XML注释进行文档化。
- 建议同时对内部成员进行文档注释,尤其是当它们逻辑复杂或无法自解释时。
Guidance for all APIs
所有API的编写指南
- Use to provide a brief, one sentence, description of what the type or member does. Start the summary with a present-tense, third-person verb.
<summary> - Use for additional information, which can include implementation details, usage notes, or any other relevant context.
<remarks> - Use for language-specific keywords like
<see langword>,null,true,false,int, etc.bool - Use for inline code snippets.
<c> - Use for usage examples on how to use the member.
<example>- Use for code blocks.
<code>tags should be placed within an<code>tag. Add the language of the code example using the<example>attribute, for example,language.<code language="csharp">
- Use
- Use to reference other types or members inline (in a sentence).
<see cref> - Use for standalone (not in a sentence) references to other types or members in the "See also" section of the online docs.
<seealso> - Use to inherit documentation from base classes or interfaces.
<inheritdoc/>- Unless there is major behavior change, in which case you should document the differences.
- 使用提供类型或成员功能的简短单句描述。摘要以一般现在时、第三人称动词开头。
<summary> - 使用添加额外信息,可包含实现细节、使用说明或其他相关上下文。
<remarks> - 使用引用特定语言关键字,如
<see langword>、null、true、false、int等。bool - 使用标记内联代码片段。
<c> - 使用添加成员的使用示例。
<example>- 使用标记代码块。
<code>标签应放在<code>标签内,使用<example>属性指定代码示例的语言,例如language。<code language="csharp">
- 使用
- 使用在句子中内联引用其他类型或成员。
<see cref> - 使用在在线文档的“另请参阅”部分添加独立(非句子内)的其他类型或成员引用。
<seealso> - 使用从基类或接口继承文档。
<inheritdoc/>- 除非存在重大行为变更,否则应在文档中说明差异。
Methods
方法
- Use to describe method parameters.
<param>- The description should be a noun phrase that doesn't specify the data type.
- Begin with an introductory article.
- If the parameter is a flag enum, start the description with "A bitwise combination of the enumeration values that specifies...".
- If the parameter is a non-flag enum, start the description with "One of the enumeration values that specifies...".
- If the parameter is a Boolean, the wording should be of the form "to ...; otherwise,
<see langword="true" />.".<see langword="false" /> - If the parameter is an "out" parameter, the wording should be of the form "When this method returns, contains .... This parameter is treated as uninitialized.".
- Use to reference parameter names in documentation.
<paramref> - Use to describe type parameters in generic types or methods.
<typeparam> - Use to reference type parameters in documentation.
<typeparamref> - Use to describe what the method returns.
<returns>- The description should be a noun phrase that doesn't specify the data type.
- Begin with an introductory article.
- If the return type is Boolean, the wording should be of the form "if ...; otherwise,
<see langword="true" />.".<see langword="false" />
- 使用描述方法参数。
<param>- 描述应为名词短语,无需指定数据类型。
- 以冠词开头。
- 如果参数是标志枚举,描述以“一个指定……的枚举值按位组合”开头。
- 如果参数是非标志枚举,描述以“一个指定……的枚举值”开头。
- 如果参数是布尔类型,表述格式为“表示……;否则为
<see langword="true" />。”<see langword="false" /> - 如果参数是“out”参数,表述格式为“当此方法返回时,包含……。此参数被视为未初始化状态。”
- 使用在文档中引用参数名称。
<paramref> - 使用描述泛型类型或方法的类型参数。
<typeparam> - 使用在文档中引用类型参数。
<typeparamref> - 使用描述方法的返回值。
<returns>- 描述应为名词短语,无需指定数据类型。
- 以冠词开头。
- 如果返回类型是布尔类型,表述格式为“如果……则返回;否则返回
<see langword="true" />。”<see langword="false" />
Constructors
构造函数
- The summary wording should be "Initializes a new instance of the <Class> class [or struct].".
- 摘要的表述应为“初始化<Class>类[或结构体]的新实例。”
Properties
属性
- The should start with:
<summary>- "Gets or sets..." for a read-write property.
- "Gets..." for a read-only property.
- "Gets [or sets] a value that indicates whether..." for properties that return a Boolean value.
- Use to describe the value of the property.
<value>- The description should be a noun phrase that doesn't specify the data type.
- If the property has a default value, add it in a separate sentence, for example, "The default is ".
<see langword="false" /> - If the value type is Boolean, the wording should be of the form "if ...; otherwise,
<see langword="true" />. The default is ...".<see langword="false" />
- 应以下列形式开头:
<summary>- 读写属性使用“获取或设置……”。
- 只读属性使用“获取……”。
- 返回布尔值的属性使用“获取[或设置]一个指示是否……的值”。
- 使用描述属性的值。
<value>- 描述应为名词短语,无需指定数据类型。
- 如果属性有默认值,在单独的句子中说明,例如“默认值为”。
<see langword="false" /> - 如果值类型是布尔类型,表述格式为“如果……则为;否则为
<see langword="true" />。默认值为……”<see langword="false" />
Exceptions
异常
- Use to document exceptions thrown by constructors, properties, indexers, methods, operators, and events.
<exception cref> - Document all exceptions thrown directly by the member.
- For exceptions thrown by nested members, document only the exceptions users are most likely to encounter.
- The description of the exception describes the condition under which it's thrown.
- Omit "Thrown if ..." or "If ..." at the beginning of the sentence. Just state the condition directly, for example "An error occurred when accessing a Message Queuing API."
- 使用记录构造函数、属性、索引器、方法、运算符和事件抛出的异常。
<exception cref> - 记录所有由成员直接抛出的异常。
- 对于嵌套成员抛出的异常,仅记录用户最可能遇到的异常。
- 异常描述应说明触发抛出的条件。
- 不要以“当……时抛出”或“如果……”开头,直接陈述条件,例如“访问消息队列API时发生错误。”