Start learning free

LESSON 3 OF 347 · WELCOME TO C#

Comments in C#: Single-Line and Multi-Line

Lesson Read the full explanation

TL ; DR:

  • Comments are lines of code that C# will intentionally ignore (not execute). They don't do anything.


  • You add a single line comment in C# by typing // followed by any text:

    // This is a comment!
    Console.WriteLine("This is not!");
    


Full lesson:

Comments are lines of code that C# will intentionally ignore. They don't do anything.

They're just used to create notes for yourself and others about what the code does.

There are two types of code comments in C#:


1. Single line comments:

We create single line comments by typing // and C# will ignore (not execute) any text between // and the end of the line.

Single line comments automatically end at the next line of code:

// Next line will greet the user:
Console.WriteLine("Hello user!");

Output of this code:

Hello user!

We can also use single line comments at the end of a line to explain the code:

Console.WriteLine("Hello user!"); // This line greets the user
Console.WriteLine("This is not a comment!");

Output of this code:

Hello user!
This is not a comment!

2. Multi-line comments

We can also comment multiple lines of code using multi-line comments.

We type /* to begin the comment and type */ to end the comment:

/* This is a 
multi-line comment
Console.WriteLine("This will not run");
Next line will greet the user:
*/

Console.WriteLine("Hello user!");

Output of this code:

Hello user!

Assignment
Follow the Coding Tutorial and let's write some comments!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · WELCOME TO C#

    Comments in C#, explained

    What comments are

    A comment is text in your source code that the C# compiler ignores. It never runs and never affects the program. Comments exist for people: to explain what a piece of code does, to leave a note for yourself, or to switch a line off for a moment while you test something.

    C# has two kinds of ordinary comments, a single-line comment and a multi-line comment.

    Single-line comments

    Type // and everything from there to the end of the line is ignored. The comment ends by itself at the line break, so the next line of code is back to being normal code:

    // The next line greets the user:
    Console.WriteLine("Hello user!");
    
    Hello user!
    

    A comment can also follow code on the same line:

    Console.WriteLine("Hello user!"); // This line greets the user
    Console.WriteLine("This is not a comment!");
    
    Hello user!
    This is not a comment!
    

    The code before the // runs as usual. Only the text after it is ignored, and only for that one line.

    Multi-line comments

    To ignore several lines, start with /* and end with */. Everything between the two markers is ignored, no matter how many lines it covers:

    /* This is a
    multi-line comment.
    Console.WriteLine("This will not run");
    The next line greets the user:
    */
    Console.WriteLine("Hello user!");
    
    Hello user!
    

    Even the Console.WriteLine("This will not run"); line inside the comment is ignored, which is why commenting out code is a handy debugging trick.

    A multi-line comment can also sit inside a line, such as Console.WriteLine(x /* the count */ + 1);, but that style is rare.

    Gotchas in C#

    • Close every /*. If you forget the */, the comment runs to the end of the file and the compiler reports an error such as "End-of-file found, '*/' expected".
    • Multi-line comments don't nest. The first */ ends the comment, even if you opened another /* inside. Anything after that first */ is read as code again.
    • // inside a string is not a comment. In Console.WriteLine("http://x.com"); the slashes are part of the text and are printed.
    • Use // for notes. It is the usual choice in C#, and it can't leave a comment open by accident. Save /* ... */ for switching off a block.
    • Documentation comments. A comment starting with /// before a method or class is a special documentation comment that tools can read. For now, treat it as a normal comment.
    • Comment the "why". A comment that repeats the code ("add one to count") adds nothing. A note on why the code does something helps the next reader. And when code changes, update its comments, because a stale one is worse than none.

    The exercise

    You are given a program that prints a few lines about Andy, with no explanation of what it is. The task is to document it with a single-line comment on line 1, above the print lines, saying Story of Andy.

    1. On the first line, type //, a space, and then the text Story of Andy.
    2. Leave the print lines as they are. A comment must not use Console.WriteLine, because the text should be ignored, not printed.
    // Story of Andy
    
    Console.WriteLine("My name is Andy");
    Console.WriteLine("I started coding at 15 years old");
    // ...the remaining print lines stay exactly as they were
    

    When the program runs, the output is the same as before. The comment changes nothing about what is printed, which is how you can tell it worked: the lines print, and "Story of Andy" does not.

    Key takeaways

    • Comments are ignored by the compiler and are written for people.
    • // comments out the rest of a line.
    • /* ... */ comments out everything between the markers, across lines.
    • An unclosed /* is a compile error, and // inside a string is plain text.
    • Good comments explain why, and they stay up to date.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free