Comments are notes you leave in your code for yourself and other developers. The compiler ignores them completely — they're purely for human readers. Good comments explain why something is done, not what the code does (the code already shows that).
Single-Line Comments (//)
Two forward slashes comment out everything after them on that line:
// This is a single-line comment
int x = 10; // You can put comments at the end of a line too
// Console.WriteLine("This won't run"); // Useful for temporarily disabling code
Single-line comments are perfect for quick explanations or temporarily removing a line during debugging.
Multi-Line Comments (/* */)
When you need more than one line, use the /* */ syntax. Everything between the opening and closing markers is commented out:
/* This is a multi-line comment.
It can span several lines.
Everything here is ignored by the compiler. */
/*
This is also useful for commenting out
large blocks of code during debugging:
int a = 1;
int b = 2;
int c = a + b;
Console.WriteLine(c);
*/
XML Documentation Comments (///)
C# has a special type of comment for documenting classes, methods, and properties. These use three slashes and XML tags:
/// <summary>
/// Calculates the area of a circle given its radius.
/// </summary>
/// <param name="radius">The radius of the circle</param>
/// <returns>The area as a double</returns>
static double CircleArea(double radius) {
return Math.PI * radius * radius;
}
When you use this style, Visual Studio and VS Code show those descriptions in IntelliSense tooltips. Other developers see your documentation right as they're typing code.
You'll see /// comments in professional codebases and open-source libraries. They're optional but incredibly helpful for public APIs.
When to Comment
Here's my rule of thumb: comment the why, not the what. Reading // Adds 1 to x above x++ is useless — the code already shows that. But // Increment x because the loop tracks attempts tells me the purpose.
And remember: the best comment is often a well-named variable or method. int userAge is clearer than int a with a comment explaining what a is.