What comments are
A comment is text in your source code that the C# compiler ignores. It never runs and never affects the program. Comments exist for people: to explain what a piece of code does, to leave a note for yourself, or to switch a line off for a moment while you test something.
C# has two kinds of ordinary comments, a single-line comment and a multi-line comment.
Single-line comments
Type // and everything from there to the end of the line is ignored. The comment ends by itself at the line break, so the next line of code is back to being normal code:
// The next line greets the user:
Console.WriteLine("Hello user!");
Hello user!
A comment can also follow code on the same line:
Console.WriteLine("Hello user!"); // This line greets the user
Console.WriteLine("This is not a comment!");
Hello user!
This is not a comment!
The code before the // runs as usual. Only the text after it is ignored, and only for that one line.
Multi-line comments
To ignore several lines, start with /* and end with */. Everything between the two markers is ignored, no matter how many lines it covers:
/* This is a
multi-line comment.
Console.WriteLine("This will not run");
The next line greets the user:
*/
Console.WriteLine("Hello user!");
Hello user!
Even the Console.WriteLine("This will not run"); line inside the comment is ignored, which is why commenting out code is a handy debugging trick.
A multi-line comment can also sit inside a line, such as Console.WriteLine(x /* the count */ + 1);, but that style is rare.
Gotchas in C#
- Close every
/*. If you forget the*/, the comment runs to the end of the file and the compiler reports an error such as "End-of-file found, '*/' expected". - Multi-line comments don't nest. The first
*/ends the comment, even if you opened another/*inside. Anything after that first*/is read as code again. //inside a string is not a comment. InConsole.WriteLine("http://x.com");the slashes are part of the text and are printed.- Use
//for notes. It is the usual choice in C#, and it can't leave a comment open by accident. Save/* ... */for switching off a block. - Documentation comments. A comment starting with
///before a method or class is a special documentation comment that tools can read. For now, treat it as a normal comment. - Comment the "why". A comment that repeats the code ("add one to count") adds nothing. A note on why the code does something helps the next reader. And when code changes, update its comments, because a stale one is worse than none.
The exercise
You are given a program that prints a few lines about Andy, with no explanation of what it is. The task is to document it with a single-line comment on line 1, above the print lines, saying Story of Andy.
- On the first line, type
//, a space, and then the textStory of Andy. - Leave the print lines as they are. A comment must not use
Console.WriteLine, because the text should be ignored, not printed.
// Story of Andy
Console.WriteLine("My name is Andy");
Console.WriteLine("I started coding at 15 years old");
// ...the remaining print lines stay exactly as they were
When the program runs, the output is the same as before. The comment changes nothing about what is printed, which is how you can tell it worked: the lines print, and "Story of Andy" does not.
Key takeaways
- Comments are ignored by the compiler and are written for people.
//comments out the rest of a line./* ... */comments out everything between the markers, across lines.- An unclosed
/*is a compile error, and//inside a string is plain text. - Good comments explain why, and they stay up to date.