Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
singleline
multiline
documentation
communitycreator

What are comments in Dart programming?

Maria Elijah

Comments are a set of statements that make source code more readable. Comments typically provide information or explanations regarding a variable, method, class, or any other statement in the source code. During the program’s execution, comment statements are ignored.

In general, comments provide a quick overview of what is going on in the code.

Dart programming supports three kinds of comments:

  1. Single-line comments
  2. Multi-line comments
  3. Documentation comments

Single-line comments

A single-line comment extends up to the newline character and is specified with a double forward slash (//). A single-line comment can be used until a line break is reached.

Syntax

The syntax for a single-line comment is as follows.

// A single comment line

Example

The example below shows how you can use single-line comments in your code.

void main(){  
    // Example of a single-line comment
    // Prints the given statement on screen  
    print("This is a shot on Dart Comments");  
}  

Multi-line comments

Multi-line comments can be used when you need to apply comments to multiple lines.

Syntax

The syntax for a multi-line comment is as follows.

/*
This is a 
multi-line  
comment 
*/

The compiler disregards everything between /* and */. However, a multi-line comment cannot be nested within other multi-line comments.

Example

The example below shows how you can use multi-line comments in your code.

void main(){  
    /* An example of a multi-line comment 
    Prints the given statement on screen */  
      
    print("This is a shot on Dart Comments");  
}  

Documentation comments

Documentation comments are a special form of comments that are mostly used to create documentation or a reference for a project or software package.

A documentation comment can be a single-line or multi-line comment that contains the /// or /* characters at the beginning. You can use /// on successive lines, which is the same as the multi-line comment. Except for lines written inside the curly brackets, the Dart compiler ignores these lines.

Syntax

The syntax for a documentation comment is as follows

///This  
///is   
///an example of  
/// a documentation comment 

Example

The example below shows how you can use documentation comments in your code.

void main(){  
    /// This  
    /// is   
    /// an example of  
    /// a documentation comment
    /// Prints the given statement on screen
      
    print("This is a shot on Dart Comments");  
}  

RELATED TAGS

dart
singleline
multiline
documentation
communitycreator
RELATED COURSES

View all Courses

Keep Exploring