Comment (C#)

This page covers Comment in C#. For a language-agnostic introduction, see Comment.

A comment in C# is text the compiler ignores. C# has two comment syntaxes: single-line and block.

Single-line comment#

A single-line comment starts with // and continues to the end of the line.

// Everything on this line is a comment.
Console.WriteLine("Hello, World!");

You can also write it inline, to the right of code:

Console.WriteLine("Hello, World!"); // Everything to the right is a comment.

Block comment#

A block comment starts with /* and ends with */. Everything between those markers is ignored, regardless of how many lines it spans.

/*
This is a block comment.
It can span multiple lines.
*/
Console.WriteLine("Hello, World!");

Block comments also work on a single line or inline:

/* Block comment on a single line */
Console.WriteLine("Hello, World!"); /* Block comment inline */

Always close a block comment with */. If you forget, everything after the opening /* — including real code — will be treated as part of the comment and ignored.

Common Mistakes#

Using # instead of // # is the comment syntax in Python. In C#, # is used for preprocessor directives — not comments. Single-line comments in C# always start with //.

Nesting block comments C# does not support nested block comments. Writing /* outer /* inner */ */ will cause a compile error — the first */ closes the comment, and the remaining */ is unexpected code.

Leaving block comments unclosed An unclosed /* silently comments out everything that follows it in the file. The compiler won’t always give a clear error — it may just report errors on lines that look fine, because they’re being treated as comment text.

Resources#