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.

Summary

This lesson introduces comments in C#, which are annotations in code to improve readability for yourself and other developers. Comments are completely ignored by the compiler and have no impact on code execution. The lesson covers two types of comments: single-line comments using // and multi-line comments using /* */ syntax, along with best practices for writing clear and precise comments that enhance code comprehension in team environments.

Key points

  • Comments in C# are ignored by the compiler and don't affect code execution
  • Single-line comments are created using // syntax, while multi-line comments use /* */ and can span several lines
  • Comments significantly improve code readability for both yourself and other developers working on the same project
  • Write clear, precise comments that accurately describe what code does rather than vague explanations
  • Comments are especially valuable when returning to code after time away or when collaborating in team projects
  • Multi-line comments are useful for commenting out code sections during testing or for explaining complex logic across multiple lines

FAQ

Do comments affect the execution of my C# code?

No, comments are completely ignored by the compiler and have no impact whatsoever on how your program runs or its performance.

What's the difference between // and /* */ comments?

// creates a single-line comment where everything after it on that line is ignored, while /* */ allows you to comment out multiple lines of code in one block.

Why are comments important in professional development?

Comments make code understandable for yourself when you revisit it later and for team members working on the same project. They clarify the purpose and logic of functions and complex code sections.