English is not my mother tongue, this page may contain incorrect words or sentences.
urriellu.net => Articles => Software => Documentation in C#
When coding in C# you can write one-line comments beginning by "//" or multiline comments between "/*" and "*/". There is no need for adding an asterisk at the beginning of each line but Visual Studio does it automatically and it is more clean.
cvl-en.cs, 6 lines [download]
- //comment
- int a = 1; //another comment
- /* multi-
- * line
- * comment */
- int b = a;
But what is really interesting is the ability to add documentation directly into the source code. You can write well-structured XML comments and they will help you when autocompleting names of classes, objects, variables or anything else, and you can build the documentation for developers (which explains the code) automatically using those comments.
Note: all these labels are optional. They are just a few of them.
Documentating classes
cls-en.cs, 11 lines [download]
- ///<summary>
- /// Main Class
- ///</summary>
- ///<remarks>
- /// Reads config files and creates threads that executes the rest of the app
- ///</remarks>
- class CApp {
- static void Main(string[] args) {
- ...
- }
- }
Between <summary> and </summary> you can add a summary or description of the class. Labels <remarks> and </remarks> are used for special comments about the class, they won't be shown when autocompleting but they will appear on the documentation.
Documentating members
miem-en.cs, 10 lines [download]
- class CApp2 {
- ///<summary>
- ///Amount of retries when accessing a file
- ///</summary>
- ///<remarks>
- ///It can be modified at any time
- ///</remarks>
- int var = 1;
- ...
- }
The same as classes.
Documentating methods/functions
met-en.cs, 14 lines [download]
- ///<summary>
- ///Reads config files
- ///</summary>
- ///<return>
- ///true if the config file was successfully read. false in case of error
- ///</return>
- ///<param name="file">
- ///Path to the file that contains the configuration parameters
- ///</param>
- public bool ReadConfig(string file) {
- bool c = false;
- ...
- return c;
- }
Between <return> and </return> the value returned by the function is explained, and <param> labels are used for describing each one of the parameters sent to the function.
Generating documentation
Documentation files can be also generated. On Windows using .NET Framework implementation by Microsoft you can do:
On Unix-like systems running Mono:
And on Windows running Mono:
Automatic documentation
There is an add-in for MS Visual Studio 2003 and 2005, GhostDoc, which allows you to add documentation comments using a simple keyboard shortcut for any kind of member you are documentating (a class, method, variable...).
ProjectsElectronicsCLK³pic³progMechatronicsSoftwarePigmeoOthermabxArticlesElectronicsSoftwareOtherAdvantages of C#Events2nd (inter)Universitary Free Software ContestMicrobotics championship at the University of Oviedo. Summer 2007About urriellu.netThis websiteAbout mePublishing licensesContact
languageespañolrot13morse codebrailleenglishrot13morse codebraille