Introduction :
Comments are equally important in any programming language. Comments are ignored by the compiler i.e. they are not executed by the compiler and they don’t provide any impact on the output of the program.
Usually, comments are used to provide useful information in code that helps in code readability. In production build, comments are removed by the compiler. For example, if your dart application has a lot of comments and you build one production android apk, the comments will be removed.
Dart supports single-line, multi-line, and documentation comments. In this tutorial, I will show you how to use comments in dart with examples :
Single-line comments :
Single-line comments begin with //. Everything starts between // and end of the line is ignored by the dart compiler. For example :
main(List<string> args) {
// This is a single line comment
var i = 10; // This is also a single line comment
// This is a single line comment
// writing on multiple lines
}
We can write multiple lines that start with // to make it looks like a multiline comment. Dart also provides multi-line commenting that I am showing below :
Multi-line comments :
Multi-line comments should be enclosed between /* and */. Everything in between these two symbols will be ignored by the dart compiler. Multiline comments are useful for commenting out a large piece of code. For example :
/*
This is a multiline
comment
*/
main(List<string> args) {
/*
var i = 0;
i ++;
i --;
*/
}
Documentation comments :
Documentation comments are used mainly for documenting software. The document generation software uses these comments to prepare the document.
Documentation comments can be a multiline or single line. It starts with /// or /**. For example :
class Student{
}
/// This is the main method and it is called
/// first
main(List<string> args) {
/**
* Creating a [Student] object
*/
var student = Student();
}
The compiler ignores all text unless we put something in an enclosed bracket. We can place fields, top-level variables, methods, class, parameters or functions inside a bracket. The document generator program links that to its own documentation. For example, in the above program, [Student] will be linked to the documentation of Student class.