C# Tutorial C# Advanced C# Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C# - Comments



The Comments are added in programming code with the purpose of making the code easier to understand. It makes the code more readable and hence easier to update the code later. Comments are ignored by compiler while running the code. In C#, there are two ways of putting a comment.

  • Single line comment
  • Block comment

Single Line Comment

It starts with // and ends with the end of that line. Anything after // to the end of line is a single line comment and will be ignored by compiler.

Syntax

//single line comment.
statements; //single line comment.

Example:

The example below illustrates how to use single line comments in a C# code. The compiler ignores the comments while compiling the code.

using System;

class MyProgram {
  static void Main(string[] args) {
    //first single line comment.
    Console.WriteLine("Hello World!"); //second single line comment.
  }
}

The output of the above code will be:

Hello World!

Block Comment

It starts with /* and ends with */. Anything between /* and */ is a block comment and will be ignored by compiler.

Syntax

/*block 
comment.*/
statements; /*block comment.*/ statements;

Example:

The example below describes how to use multiple line comments in a C# code which is ignored by the compiler while compiling the code.

using System;

class MyProgram {
  static void Main(string[] args) {
    /*first 
    block comment. */
    Console.WriteLine(/*second block comment.*/"Hello World!");
  }
}

The output of the above code will be:

Hello World!

5