C# Comments

Comments are notes you write inside your code that the computer ignores when the program runs. They are for humans, helping you and others understand what the code does. In this lesson you will learn the three kinds of comments in C#.

Single-line comments

A single-line comment starts with two forward slashes (//). Everything after the slashes on that line is ignored by C#. You can put a comment on its own line or at the end of a line of code.

Using single-line comments

using System;

class Program
{
    static void Main()
    {
        // This line prints a greeting
        Console.WriteLine("Hello World!");
        Console.WriteLine("Learning comments"); // printed after the greeting
    }
}

Multi-line comments

When you need a comment that spans several lines, you can start it with /* and end it with */. Everything between those symbols is ignored, no matter how many lines it covers.

Using a multi-line comment

using System;

class Program
{
    static void Main()
    {
        /* This comment spans
           more than one line and
           explains the code below */
        Console.WriteLine("Comments are ignored by C#");
    }
}
Note: Comments do not affect how your program runs at all. The compiler skips over them completely.

Why use comments?

  • To explain what a piece of code is meant to do.
  • To leave reminders or notes for yourself and other developers.
  • To temporarily disable a line of code without deleting it.
  • To make complex programs easier to read and maintain.

A common trick is to comment out a line so it does not run, which is useful while testing. In the example below, the second message will not appear.

Commenting out code

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("This line runs");
        // Console.WriteLine("This line is turned off");
    }
}
Comment typeSymbols
Single-line// note here
Multi-line/* note here */

Exercise: C# Comments

What symbol starts a single-line comment in C#?