当前位置:网站首页>C# 注释语法
C# 注释语法
2022-08-02 03:27:00 【__Benco】
/// <summary>
/// "这里写内容,这个方法干什么的"
/// </summary>
/// <param name="sender">"是方法的名字"</param>
/// <returns>(是返回值~ 例如返回一个int等)</returns>
注释语法
为了使用C#提供的XML注释功能,你的注释应该使用特殊的注释语法“///”开头。在“///”之后,你可以使用预先定义的标签注释你的代码,也可以插入你自己定义的标签。你定制的标签将会在随后加入到生成的注释文档中。
预定义的标签 用处
<c> 将说明中的文本标记为代码
<code> 提供了一种将多行指示为代码的方法
<example> 指定使用方法或其他库成员的示例
<exception> 允许你指定可能发生的异常类
<include> 允许你引用描述源代码中类型和成员的另一文件中的注释,使用 XML XPath 语法来描述你的源代码中的类型和成员。
<list> 向XML注释文档中插入一个列表
<para> 向XML注释文档中插入一个段落
<param> 描述一个参数
<paramref> 提供了一种指示一个词为参数的方法
<permission> 允许你将成员的访问许可加入到文档中
<remarks> 用于添加有关某个类型的信息
<returns> 描述返回值
<see> 指定链接
<seealso> 指定希望在“请参见”一节中出现的文本
<summary> 类型或类型成员的通用描述
<value> 描述属性
例子
下面的例子为我们常见的HelloWorld控制台应用程序添加注释:
using System;
namespace HelloWorld
{
/// <summary>
/// Sample Hello World in C#
/// </summary>
public class HelloWorld
{
/// <summary>
/// Console Application Entry Point
/// <param name="args">Command Line Arguments</param>
/// <returns>Status code of 0 on successful run</returns>
/// </summary>
public static int Main(string[] args)
{
System.Console.WriteLine("HelloWorld");
string name = System.Console.ReadLine();
return(0);
}
}
}
为生成XML注释文档,我们在调用csc编译源代码时使用/doc选项:
csc /doc:HelloWorld.xml helloworld.cs
生成的结果文档如下:
<?xml version="1.0"?>
<doc>
<assembly>
<name>XMlComment</name>
</assembly>
<members>
<member name="T:HelloWorld.HelloWorld">
<summary>
Sample Hello World in C#
</summary>
</member>
<member name="M:HelloWorld.HelloWorld.Main(System.String[])">
<summary>
Console Application Entry Point
<param name="args">Command Line Arguments</param>
<returns>Status code of 0 on successful run</returns>
</summary>
</member>
</members>
</doc>
HTML页面
你可能会问自己:我应该如何才能得到具有良好格式的HTML页面呢?很简单,你可以编写自己的XSL来转换生成的XML注释文档,或者使用Visual Studio.NET开发工具。通过使用VS.NET的【工具】菜单中的【生成注释web页】,你可以得到一系列详细说明你的项目或解决方案的HTML页面。下面就是通过VS.NET生成的注释helloWorld程序的HTML页面快照:
边栏推荐
猜你喜欢
随机推荐
OpenCore 黑苹果安装教程
张量乘积—实验作业
The first time to tear the code by hand, how to solve the problem of full arrangement
3000字入门图神经网络
Laravel 登录,中间件和路由分组
会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核
什么是广告电商商业模式?这几个门派告诉你
Binder机制详解(三)
Binder机制详解(二)
元宇宙:为何互联网大佬纷纷涉足?元宇宙跟NFT是什么关系?
自定义view实现半圆弧进度条
还原最真实、最全面的一线大厂面试题
管理会计(对内)指引、管理会计要素及其具体内容(可能考,考前记一下,推荐记一下四个大点即可)、
英语每日打卡
元宇宙是一个炒作的科幻概念,还是互联网发展的下半场?
解密:链动2+1的商业模式
PHP hash加密与解密
Binder机制详解(一)
Win10 解决AMD平台下SVM无法开启的问题
(3) string