๐ŸŽ“ A1Learner
๐Ÿ”

C# Comments

A comment is a note in C# code that the compiler ignores. Comments explain the code to people who read it.

Line comments

A line comment starts with two slashes. The comment sits above the print line.

using System;

class Program
{
    static void Main(string[] args)
    {
        // Print a greeting
        Console.WriteLine("Hello");
    }
}

A blocked statement

The block comment wraps one print line. That line does not run.

using System;

class Program
{
    static void Main(string[] args)
    {
        /* Console.WriteLine("Hidden line"); */
        Console.WriteLine("Visible line");
    }
}

A trailing comment

A comment can sit after code on the same line. The code still runs.

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Still runs"); // Print the words
    }
}

Example

using System;

class Program
{
    static void Main(string[] args)
    {
        // Print the plan title
        Console.WriteLine("Plan");
        /* Print the plan step
           The comment prints nothing. */
        Console.WriteLine("Step one");
    }
}

Key points

  • Two slashes start a comment that runs to the end of the line.
  • Slash and star marks start a comment that can span many lines.
  • Star and slash marks end a block comment.
  • A commented-out statement does not run.
  • A program prints the same output with or without comments.

In this example, the first print line shows Plan. A block comment sits between the print lines and prints nothing. The second print line shows Step one.

Note

Comments print nothing at all.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Programiz C# Compiler

Exercise

What does the compiler do with a comment?