给代码添加注释,表面上看来十分简单,但实际可能很复杂。
1、C#使用传统的C风格注释方式:
a、单行注释使用//... ,从//开始到行尾的内容都会被编译器忽略。
b、多行注释使用/*...*/ , /*和*/之间的所有内容会被忽略。
2、XML文档
根据特定的注释自动创建XML格式的文档说明,这些注释都是单行注释,但都是以///开头的,
我们根据下面的代码来了解它们的工作方式:
1
//
Math.cs
2 namespace Wrox
3 {
4
5 /// <summary>
6 /// Wrox.Math class.
7 /// Provides a method to add two integers.
8 /// </summary>
9 public class MathLib
10 {
11 /// <summary>
12 /// The Add method allows us to add two integers
13 /// </summary>
14 /// <returns> Result of the addition (int) </returns>
15 /// <param name="x"> First number to add </param>
16 /// <param name="y"> Second number to add </param>
17 public int Add( int x, int y)
18 {
19 return x + y;
20 }
21 }
22 }
2 namespace Wrox
3 {
4
5 /// <summary>
6 /// Wrox.Math class.
7 /// Provides a method to add two integers.
8 /// </summary>
9 public class MathLib
10 {
11 /// <summary>
12 /// The Add method allows us to add two integers
13 /// </summary>
14 /// <returns> Result of the addition (int) </returns>
15 /// <param name="x"> First number to add </param>
16 /// <param name="y"> Second number to add </param>
17 public int Add( int x, int y)
18 {
19 return x + y;
20 }
21 }
22 }
要让编译器为程序集生成XML文档,需在编译时指定/doc选项,后跟要创建的文件名:
csc /t:library /doc:MathLibrary.xml MathLib.cs
1
<?
xml version
=
"
1.0
"
?>
2 < doc >
3 < assembly >
4 < name > MathLibrary </ name >
5 </ assembly >
6 < members >
7 < member name = " T:Wrox.MathLib " >
8 < summary >
9 Wrox.Math class .
10 Provides a method to add two integers.
11 </ summary >
12 </ member >
13 < member name = " M:Wrox.MathLib.Add(System.Int32,System.Int32) " >
14 < summary >
15 The Add method allows us to add two integers
16 </ summary >
17 < returns > Result of the addition ( int ) </ returns >
18 < param name = " x " > First number to add </ param >
19 < param name = " y " > Second number to add </ param >
20 </ member >
21 </ members >
22 </ doc >
2 < doc >
3 < assembly >
4 < name > MathLibrary </ name >
5 </ assembly >
6 < members >
7 < member name = " T:Wrox.MathLib " >
8 < summary >
9 Wrox.Math class .
10 Provides a method to add two integers.
11 </ summary >
12 </ member >
13 < member name = " M:Wrox.MathLib.Add(System.Int32,System.Int32) " >
14 < summary >
15 The Add method allows us to add two integers
16 </ summary >
17 < returns > Result of the addition ( int ) </ returns >
18 < param name = " x " > First number to add </ param >
19 < param name = " y " > Second number to add </ param >
20 </ member >
21 </ members >
22 </ doc >
T表示一个类型 F表示一个字段 M表示一个成员。