Java simplified code documentation by introducing the special /** */ Javadoc comments and providing the javadoc.exe tool. C# also provides a mechanism for developers to document their code using a special comment syntax. There are two ways to insert document comments in C# codes “ the second syntax should be familiar to Java developers: [1]
While Javadoc parses the document comments to form HTML files, C#'s documentation generator creates an XML file. You can view the generated XML document with any XML file reader (Microsoft Internet Explorer is quite rudimentary but useable). In C#, your XML comments must be well- formed . [2] While javadoc.exe tolerates non-well-formed HTML tags, the C# documentation generator will complain when it encounters non-well-formed documentation comments in your source codes.
In the spirit of XML, developers are free to invent and define their own documentation tags for their own project team. The C# Language Specification lists a set of recommended documentation tags, which is shown in Table D.1. [3]
The 'verified' column in the table shows if the tag is verified by the documentation generator. For verified tags, any verification problem (for example, you specified an exception type which cannot be resolved, or a method parameter that does not exist) will be shown as warnings or errors by the documentation generator. Table D.1. Description of recommended documentation tags
Table D.2 gives the syntax of each recommended tag. Table D.2. XML syntax of recommended documentation tags
A full example is shown below. It shows a Car class containing a single constructor, two private fields called maxSpeed and currSpeed (for maximum and current speeds respectively) accessible via two public properties, MaxSpeed and CurrSpeed . Car contains two public methods “ Accelerate and Decelerate “ which alter currSpeed 's value. 1: using System; 2: 3: /// <summary> 4: /// This class is usually subclassed to more specialized 5: /// car types. Known subclasses include <c> Toyota </c> and 6: /// <c> Nissan </c> . 7: /// </summary> 8: 9: class Car{ 10: 11: private int maxSpeed; 12: private int currSpeed; 13: 14: public Car (int maxSpeed){ 15: this.maxSpeed = maxSpeed; 16: } 17: 18: /// <remarks> 19: /// Invoke this method to increase the current speed 20: /// by a <paramref name = " factor "/> . The current speed 21: /// will never go beyond the car's maximum speed. So, 22: /// if this method is invoked when the sum of 23: /// <paramref name = " factor "/> and the current speed is 24: /// greater than the maximum speed, the current speed will 25: /// be set to the maximum speed only. 26: /// </remarks> 27: /// 28: /// <param> 29: /// <c> factor </c> 30: /// Factor you want to increase the current speed by 31: /// </param> 32: /// 33: /// <see cref = " Decelerate "> 34: /// Use <c> Decelerate </c> to decrease the current 35: /// speed by a factor. 36: /// </see> 37: /// 38: /// <returns> 39: /// An <c> int </c> which is the current speed. 40: /// </returns> 41: 42: public int Accelerate(int factor){ 43: CurrSpeed += factor; 44: if (currSpeed>maxSpeed) 45: currSpeed = maxSpeed; 46: return CurrSpeed; 47: } 48: 49: /// <remarks> 50: /// Invoke this method to decrease the current speed 51: /// by a <paramref name = " factor "/> . The current speed 52: /// will never go below zero. If this method is invoked 53: /// with the difference between the current speed and 54: /// <paramref name = " factor "/> less than zero, a 55: /// <c> InvalidSpeedAssignmentException </c> will be 56: /// thrown. 57: /// </remarks> 58: /// 59: /// <param> 60: /// <c> factor </c> 61: /// Factor you want to decrease the current speed by. 62: /// </param> 63: /// 64: /// <exception cref = " InvalidSpeedAssignmentException "> 65: /// This exception is thrown if an attempt is made to 66: /// decrease the current speed to a negative value. 67: /// </exception> 68: /// 69: /// <see cref = " Accelerate "> 70: /// Use <c> Accelerate </c> to increase the current 71: /// speed by a factor. 72: /// </see> 73: /// 74: /// <returns> 75: /// An <c> int </c> which is the current speed. 76: /// </returns> 77: 78: public int Decelerate(int factor){ 79: currSpeed -= factor; 80: if (currSpeed<0) 81: throw new InvalidSpeedAssignmentException(); 82: return currSpeed; 83: } 84: 85: /// <value> 86: /// The maximum speed of this car. <c> MaxSpeed </c> is a 87: /// get-only property. 88: /// </value> 89: 90: public int MaxSpeed{ 91: get { 92: return maxSpeed; 93: } 94: } 95: 96: /// <remarks> 97: /// <c> CurrSpeed </c> cannot be higher than 98: /// <c> MaxSpeed </c> . If you set <c> CurrSpeed </c> to a 99: /// value higher than <c>MaxSpeed </c> , <c> CurrSpeed </c> 100: /// will be automatically set to <c> MaxSpeed </c> . 101: /// </remarks> 102: /// 103: /// <value> 104: /// The current speed of this car. <c> CurrSpeed </c> 105: /// is a get-set property. 106: /// </value> 107: 108: public int CurrSpeed{ 109: get { 110: return currSpeed; 111: } 112: set { 113: if (value>maxSpeed) 114: currSpeed = maxSpeed; 115: else 116: currSpeed = value; 117: } 118: } 119: } 120: 121: class InvalidSpeedAssignmentException 122: :ApplicationException{ 123: } If you are using csc.exe , it comes with a fully fledged documentation generator. Use the /doc option to generate the XML documentation of a source file: c:\expt>csc /doc:TestClass.xml TestClass.cs In this case, because my class does not contain a Main method, and is meant to be compiled into a DLL assembly, I compiled using the /target:library option too: c:\expt>csc /doc:TestClass.xml /t:library TestClass.cs On successful compilation, I get TestClass.dll and TestClass.xml . Part of the XML document generated from the above source file is shown in an Internet Explorer window in Figure D.1 Figure D.1. Showing a high-level view of the XML documentation generated.
Notice that additional tags such as <assembly> have been inserted for you. Each member name attribute is given a value prefixed by a letter “ T is for type, M for method, P for property, F for field, E for event, and N for namespace. Let's 'expand' out the Decelerate method. This is shown in Figure D.2. Figure D.2. XML documentation generated.
Other tools are needed to convert the XML documentation file into something more presentation friendly. If you are using VS .NET then under the Tools menu select Build Comment Web Pages , and the corresponding HTML pages will be created for you. csc.exe cannot do that for you. |