sloppycode.net
XML Comment Cheat Sheet
.NET XML Documentation guide + cheat sheet
Home
›
Code snippets
›
C#
›
XML Comment Cheat Sheet
Download .snippet files for these examples
If you like to give your code meaningful XML comments and want the output to look vaguely like the MSDN documentation, then below are a few templates you can use for consistency.These aren't "how to write XX method" but just how the textual descriptions appear in most of the .NET framework documentation, for example you'll find 'Raises the ... event' in almost all event descriptions.
The download above the examples below in snippet format (for VS.NET 2005/2008) that autocompletes various methods. To use the .snippet download, copy it to your 'C:\Users\
\Documents\Visual Studio 2008\Code Snippets\Visual C#' directory and then type eqts inside the code editor in Visual Studio.
Make sure you click 'view plain' to get the properly formatted text, as some lines span over the textarea.
Embedding tags:
If you need to put a tag inside you description. There's no need to do this for CDATA.
< >
New Paragraphs
You will need to wrap descriptions in these tags if you want it to be readable instead of a wall of text.
Standard constructor
///
/// Initializes a new instance of the
MyClass
class. ///
Standard event and raising the event
///
/// Occurs when the page is process is complete. ///
public event EventHandler Complete; ///
/// Raises the
Complete
event. ///
///
An
EventArgs
object that contains the event data. protected virtual void OnComplete(EventArgs e) { if (Complete != null) Complete(this, e); }
Embedding links to other classes
Go on, have a look at
MyClass
Embedding links to other class properties/methods
Go on, have a look at
MyMethod
and Go on, have a look at
MyProperty
Embedding an example
Remember to use CDATA or you'll get documentation errors.
///
/// Below is an example of using MyMethod to do something. ///
/// <![CDATA[ /// MyClass a = new MyClass(); /// string result = a.MyMethod(); /// ]]> ///
///
Linking to external documentation
From another file (for large amounts of documentation that makes it hard to read the source file)
then the XML file would be:
All the usual XML tags here.
etc.
Your method catches an exception
///
/// Here you describe why the exception will be thrown. ///
Exception class headers
///
/// Initializes a new instance of the
MyException
class using the /// specified message and inner exception. ///
///
The error message that explains the reason for the exception. ///
The exception that is the cause of the current exception. If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.
Overriding Hashcode
As found in MSDN
///
/// Computes and retrieves a hash code for an object. ///
///
/// This method implements the
Object
method. ///
///
A hash code for an object.
Overriding ToString
As found in MSDN
///
/// Displays information about the
Field
in readable format. ///
///
A string representation of the object.
Overriding Equals
As found in MSDN
///
/// Determines whether two
X
objects are equal. ///
///
/// For the two
X
objects to be equal, they must have the same Number. ///
///
The
X
object to compare to the current
X
. ///
True if the specified
X
is equal to the current
X
; otherwise false.
Links
Sandcastle for building web/htmlhelp documentation from the .xml files
GUI for Sandcastle
MSDN docs for .NET XML documentation (local)
MSDN docs for .NET XML documentation (online)
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search