Comments are descriptions of text for one or more lines of code.

Reading the comments may help other developers understand the code better.

Including Comments on the code blocks or lines improves readability. The compiler ignores comments during the execution.

There are several ways to write a comment in C#.

  • Single-line comments
  • Multi-Line comments
  • Documentation commands

How to write a Single line comment in C

Single-line also called inline comments, are always added as a single line.

  • In a comment line, the double slash(//) sign is always placed before the comment string.
  • A comment text explains a single line of code.
  • These comments can be written at the start, in the middle, or at the end of the line.

Syntax:

// This is single line comments in Perl Code

Example

hello.cs:

// Hello World application
using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World Message printed to console "); // Printing text to Console
    }
}

Perl Multi-line comments

Multi-line comments also called block comments, contain comment descriptions span in multiple lines.

Here is a way we can write a multi-line comment. Multi-line Comments always start with /* and are followed by single or multiple lines text and end with */

/* multi-line comments 1
 multi-line comments 2
 multi-line comments 3 
*/

You can also write multi-line comments with single-line comment syntax as given below.

= multi-line comments 
//multi-line comments 2
//multi-line comments 3
=cut

This allows you to write multiple lines of code and be ignored by the compiler.

Block level comments in Perl Example:

/* Hello World First Program
* Prints the  hello world text to the console
*/
using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World Message printed to console "); // Printing text to Console
    }
}

How to Write Documentation Comments in C

Documentation comments are useful for developing writing about the API documentation syntax.

Syntax contains XML text format of tags.

These are added to the below components.

  • Types such as Class, Structs, abstract classes, and Interfaces,
  • member variables such as fields, properties, and methods

Once commented documentation, You need to pass this to Document Generator and output Documentation file format such as HTML, XML

Documentation comments contain two types

  • Single Line Documentation comments: These comments are declared in a single line. The line starts with a triple slash (///), followed by XML documentation tags syntax

Here is an example

/// <summary>
/// Class <c>MyClass</c> are single line comments.
/// </summary>
public class MyClass{

}
  • Multi-Line Documentation comments:

Comments are declared in multiple lines enclosed in /**, followed by comment tags and values, ends with end with /

/** <summary>
 Class <c>MyClass</c> are single-line comments.
 </summary>
**/
public class MyClass{

}

Here is an example

/// <summary>
/// Class <c>Employee</c> to hold information.
/// </summary>
public class Employee
{
    /// <summary>
    /// Method <c>calculateSalary</c> Calculates Salary.
    /// </summary>
    void calculateSalary() {...}
}

Compiling the above file, Generates an XML file as given below

Outputs:

<?xml version="1.0"?>
<extradoc>
    <class name="Employee">
        <summary>
            Class Employee to hold information.
        </summary>
    </class>
    <members>
    <member name="M:Employee.calculateSalary()">
      <summary>
          Method calculateSalary Calculates Salary.
        <see cref="M:Employee.calculateSalary">calculateSalary</see> 
      </summary>
     </member>
  </members>
   
</extradoc>

You can check more tags here