csharp-docs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

C# 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
    <summary>
    to provide a brief, one sentence, description of what the type or member does. Start the summary with a present-tense, third-person verb.
  • Use
    <remarks>
    for additional information, which can include implementation details, usage notes, or any other relevant context.
  • Use
    <see langword>
    for language-specific keywords like
    null
    ,
    true
    ,
    false
    ,
    int
    ,
    bool
    , etc.
  • Use
    <c>
    for inline code snippets.
  • Use
    <example>
    for usage examples on how to use the member.
    • Use
      <code>
      for code blocks.
      <code>
      tags should be placed within an
      <example>
      tag. Add the language of the code example using the
      language
      attribute, for example,
      <code language="csharp">
      .
  • Use
    <see cref>
    to reference other types or members inline (in a sentence).
  • Use
    <seealso>
    for standalone (not in a sentence) references to other types or members in the "See also" section of the online docs.
  • Use
    <inheritdoc/>
    to inherit documentation from base classes or interfaces.
    • 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
    <param>
    to describe method parameters.
    • 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 "
      <see langword="true" />
      to ...; otherwise,
      <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
    <paramref>
    to reference parameter names in documentation.
  • Use
    <typeparam>
    to describe type parameters in generic types or methods.
  • Use
    <typeparamref>
    to reference type parameters in documentation.
  • Use
    <returns>
    to describe what the method 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 "
      <see langword="true" />
      if ...; otherwise,
      <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
    <summary>
    should start with:
    • "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
    <value>
    to describe the value of the property.
    • 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 "
      <see langword="true" />
      if ...; otherwise,
      <see langword="false" />
      . The default is ...".
  • <summary>
    应以下列形式开头:
    • 读写属性使用“获取或设置……”。
    • 只读属性使用“获取……”。
    • 返回布尔值的属性使用“获取[或设置]一个指示是否……的值”。
  • 使用
    <value>
    描述属性的值。
    • 描述应为名词短语,无需指定数据类型。
    • 如果属性有默认值,在单独的句子中说明,例如“默认值为
      <see langword="false" />
      ”。
    • 如果值类型是布尔类型,表述格式为“如果……则为
      <see langword="true" />
      ;否则为
      <see langword="false" />
      。默认值为……”

Exceptions

异常

  • Use
    <exception cref>
    to document exceptions thrown by constructors, properties, indexers, methods, operators, and events.
  • 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时发生错误。”