C# ยท Chapter 6 of 46

C# Comments

Comments are notes in your code that the compiler ignores. They help explain what your code does, making it easier for you and others to understand later.

C# supports single-line comments starting with //, and multi-line comments enclosed between /* and */.

Syntax
// single-line comment
/* multi-line
   comment */

Single-line comments

Anything after // on a line is ignored by the compiler. These are great for short explanations next to a line of code.

Multi-line comments

Text between /* and */ can span multiple lines and is often used for longer explanations or temporarily disabling blocks of code.

Example 1 (csharp)
using System;

class Program {
  static void Main() {
    // This prints a greeting
    Console.WriteLine("Hello!");
  }
}
Output
Hello!

The single-line comment is ignored during compilation.

Example 2 (csharp)
using System;

/* This program
   prints a number */
class Program {
  static void Main() {
    Console.WriteLine(42);
  }
}
Output
42

The multi-line comment describes the program above the class.

Key points

  • // starts a single-line comment.
  • /* ... */ wraps a multi-line comment.
  • Comments are ignored by the compiler.
  • Good comments explain why, not just what, the code does.
๐Ÿ’ก Note: Overusing comments to state the obvious can clutter code; write comments that add real value.

๐Ÿ“ Quick Quiz

1. Which symbol starts a single-line comment in C#?

2. How do you write a multi-line comment?

3. Are comments compiled into the executable?