Comments in C#

To indicate comments using the first method, you use /* characters at the start of the comment and */ characters at the end. These may occur on a single line, or on different lines, in which case all lines in between are part of the comment. The only thing you can ’ t type in the body of a comment is */ , because this is interpreted as the end marker. For example, the following are OK:

/* This is a comment */
/* And so...
... is this! */

The following, however, cause problems:

/* Comments often end with “*/” characters */

Here the end of the comment (the characters after “ */ ” ) will be interpreted as C# code, and errors will occur. The other commenting approach involves starting a comment with // . After that, you can write whatever you like — as long as you keep to one line! The following is OK:

// This is a different sort of comment.

The following fails, because the second line is interpreted as C# code:

// So is this,

but this bit isn’t. This sort of commenting is useful to document statements, because both can be placed on a single line:

< A statement > ; // Explanation of statement

It was stated earlier that there are two ways of commenting C# code, but there is a third type of comment in C# — although strictly speaking this is an extension of the // syntax. You can use single – line comments that start with three / symbols instead of two, like this:

/// A special comment

You may also like...