Comments
Learn to write single-line (//) and multi-line (/* */) comments in C#.
Single-line comment
Comments are used to add notes to a program. The compiler ignores them; they produce no output at runtime.
In C# a single-line comment starts with // and runs to the end of the line:
// This is a comment
Console.WriteLine("Hello");Comments explain code and give hints to other developers (or your future self).
Multi-line comment
For comments that span multiple lines you use /* */. The comment starts with /* and ends with */.
/* This comment
spans multiple
lines */
Console.WriteLine("OK");Multi-line comments are ideal for longer explanations. You cannot nest them; one /* closes with one */.
Exercise Use both comment types
Add a // comment, a /* */ comment to the program and print Welcome! with Console.WriteLine.
Show example solution
Inside Main add //, /* */ comments and Console.WriteLine("Welcome!");.
Example solution:
using System;
class Program
{
static void Main()
{
// Greeting message
Console.WriteLine("Welcome!");
/* Program finished */
}
}
Test yourself
-
Comment marker
____ This is a commentShow answer
// This is a comment -
Which one is a multi-line comment?
- // Comment
- # Comment
- /* Comment */
- <!-- Comment -->
Show answer
/* Comment */ — Correct! /* */ is the multi-line comment marker in C#.
-
Match the comment types
- //
- Single-line comment
- /* */
- Multi-line comment
- ///
- XML documentation comment