Code comments - //
and /*
. */
C# supports two different forms of comments. Single line comments start with //
and end at the end of that line of code. Multiline comments start with /*
and end with */
. The following code shows an example of each:
// This is a single line comment.
/* This could be a summary of all the
code that's in this class.
You might add multiple paragraphs, or links to pages
like https://learn.microsoft.com/dotnet/csharp.
You could even include emojis. This example is 🔥
Then, when you're done, close with
*/
The multi-line comment can also be used to insert text in a line of code. Because these comments have an explicit closing character, you can include more executable code after the comment:
public static int Add(int left, int right)
{
return left /* first operand */ + right /* second operand */;
}
The single line comment can appear after executable code on the same line. The comment ends at the end of the text line:
return source++; // increment the source.
Some comments start with three slashes: ///
. Triple-slash comments are XML documentation comments. The compiler reads these to produce human documentation. You can read more about XML doc comments in the section on triple-slash comments.