In this tutorial you will learn about the Dart Comments and its application with practical example.
Dart Comments
Comments are a set of statements that are not executed by the compiler. The use of comments makes it easy for humans to understand the source code. Usually comments gives you inside or explanation about the variable, method, class or any statement that exists in source code. The comment statements are ignored during the execution of the program.
Types of Dart Comments
In Dart, there are 3 types of comments.
- Dart Single Line Comment
- Dart Multi Line Comment
- Dart Documentation Comment
Dart Single line Comments
A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character. This can be used to comments-out everything until a line break
Syntax:-
1 |
//This is single line comment |
Example:-
1 2 3 4 5 |
void main() { // It prints Hello World print("Hello World"); } |
Output:-
1 |
Hello World |
Dart Multi line Comments
If you want to comment multiple lines then you can do it using
/*
and */
, everything in between from /* to */ is ignored by the compiler-
Uses:-
- Useful for commenting out a section of code
- Cannot be nested within other multi-line comments
Syntax:-
1 2 3 4 5 6 |
/* This is multi line comment */ |
Example:-
1 2 3 4 5 6 7 |
void main() { /* This is a multiline comment * It prints Hello World */ print("Hello World"); } |
Output:-
1 |
Hello World |
Dart Documentation Comment
This is a special type of comment mainly used to generate documentation or reference for a project/software package. Documentation comments are multi-line or single-line comments that begin with ///
or /**
. Using ///
on consecutive lines has the same effect as a multi-line doc comment. The Dart compiler ignores all text inside a documentation comment except it is enclosed in brackets. The brackets are used to refer classes, methods, fields, top-level variables, functions, and parameters. The names in brackets are resolved in the lexical scope of the documented program element.
Uses:-
- Similar to multi-line comments but used to document dart code (classes, methods, expressions)
Syntax:-
1 2 3 4 |
/// This /// is /// documentation /// comment |