Comments In Dart

Comments are the set of statements that are ignored by the Dart compiler during the program execution. It is used to enhance the readability of the source code. Generally, comments give a brief idea of code that what is happening in the code.

Types of Comments

Dart provides three kinds of comments

  • Single-line Comments

  • Multi-line Comments

  • Documentation Comments

Single-line Comments

We can apply comments on a single line by using the // (double-slash). The single-line comments can be applied until a line break.

Example

void main(){  
    // This will print the given statement on screen  
    print("Welcome to Dart");  
}  

Output:

Welcome to Dart

The // (double-slash) statement is completely ignored by the Dart compiler and retuned the output.

Multi-line Comments

Sometimes we need to apply comments on multiple lines; then, it can be done by using /*…..*/. The compiler ignores anything that written inside the /*…*/, but it cannot be nested with the multi-line comments. Let’s see the following example.

Example

void main(){  
    /* This is the example of multi-line comment 
    This will print the given statement on screen */  
      
    print("Welcome to Dart");  
}  

Output:

Welcome to Dart

Dart Documentation Comments

  • The document comments are used to generate documentation or reference for a project/software package.

  • It can be a single-line or multi-line comment that starts with /// or /*. We can use /// on consecutive lines, which is the same as the multiline comment.

  • These lines ignore by the Dart compiler expect those which are written inside the curly brackets.

  • We can define classes, functions, parameters, and variables.

Consider the following example.

Syntax

///This  
///is   
///a example of  
/// multiline comment   

Example

void main(){  
    ///This is   
    ///the example of   
    ///multi-line comment  
    ///This will print the given statement on screen.  
    print("Welcome to Dart");  
}  

Output:

Welcome to Dart