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 */.
// 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.
using System;
class Program {
static void Main() {
// This prints a greeting
Console.WriteLine("Hello!");
}
}Hello!The single-line comment is ignored during compilation.
using System;
/* This program
prints a number */
class Program {
static void Main() {
Console.WriteLine(42);
}
}42The 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.
