Utilizor
Contact Us

C# Comments

Adding comments to code.

C# Comments

Comments can be used to explain C# code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by C# (will not be executed).

Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by C#.

Examples

Single-line Comment

Comment on its own line.

using System;

class Program
{
  static void Main(string[] args)
  {
    // This is a comment
    Console.WriteLine("Hello World!");
  }
}

End of Line Comment

Comment at the end of a line of code.

using System;

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("Hello World!"); // This is a comment
  }
}

Multi-line Comment

Comment spanning multiple lines.

using System;

class Program
{
  static void Main(string[] args)
  {
    /* The code below will print the words Hello World
    to the screen, and it is amazing */
    Console.WriteLine("Hello World!");
  }
}

Determining End of Comment

Commenting out a block of code.

using System;

class Program
{
    static void Main()
    {
        /*
        Console.WriteLine("This will not print");
        */
        Console.WriteLine("This will print");
    }
}

Documentation Comment

XML documentation comment used for generating API docs.

/// <summary>
/// This is a documentation comment
/// </summary>
class Program
{
    static void Main() { }
}