C-SHARP - 2.9 Comments

A comment is an annotation you add to your code to make it more readable — for yourself, and for the other developers you will work with. Comments are completely ignored by the compiler, so they have zero impact on how the program runs. Many developers neglect them, which is a mistake. Come back to your own code two months later, or join a project where ten other people contributed, and you will be grateful for the small "this function adds a product to the cart" notes that someone left along the way.

Two syntaxes exist in C#. For a single line, type // at the start of the line: everything after the slashes is a comment, displayed in a different colour by Visual Studio. For example, next to Console.WriteLine(...) you can add // asks the user to enter a word. For multi-line comments, use /* to open and */ to close — anything between them, on any number of lines, is ignored by the compiler.

When to use comments

  • Document intent: explain why code does what it does, not just what — the what should be obvious from the code itself.
  • Disable code temporarily: wrap a chunk in /* ... */ to test the program without it, without deleting the lines.
  • Help collaborators: in a team project, a clear comment near complex logic saves hours of "what was the author thinking?" later.

One rule: keep comments clear and precise. A vague // fix something here is worse than no comment at all. With this habit baked in early, your code stays readable as it grows and your future self (and teammates) will thank you. That wraps up this section — the next videos start tackling conditions and control flow.