Comments

Comments is a very useful piece of information to understand the code easily and it makes the code more readable. Generally we are writing comments for developers who will work on that code in future or may be for ourselves if we are checking code after long time. This will be an explanation of the code what this line/function will do. Comments will be completly ignored by compiler.

We can write the comments in different ways using single line comments, multiline comments etc.

C# Single-line Comments

Single line comment starts with a double slash //. The compiler will ignore the code after // till the end of the line. For example

Try it
int[] numbers = { 58, 20, 45, 36, 24 };
Array.Sort(numbers);  // This line will sort the array in ascending order

In above code This line will sort the array in ascending order is the comment. Single line can be written on the same line after the code or on a new line. However it is recommended that you write comment on seperate line and above the code.

C# Multi-line Comments

Multi line comment starts with /* and ends with */. It can be spread over multiple line. However it is not mandatory that multi line comments uses multiple line, we can use multi line comment for single line comment as well /* commented code */

Try it
class Program
{
    /*
      This is the entry point of program
      Author : Tutorials Corner
      Date : 26-Sept-2020
    */
    static void Main(string[] args)
    {
        int[] numbers = { 58, 20, 45, 36, 24 };
        Array.Sort(numbers);  
        foreach (int number in numbers)
        {
            Console.WriteLine("The value is " + number);
        }
    }
}
    

Try to avoid too much comments in the code. Write only useful comments where absolutely necessary.

 

← Previous

Next →