Comments
In C#, comments allow a developer to add explanatory or documenting notes to code. They do not affect program execution and are ignored by the compiler. C# has three types of comments:
Single-line comments
Single-line comments start with two characters:
//. Everything after these characters until the end of the line is considered a comment. Line 1, Char 1
Multi-line comments
Multi-line comments use the
/* ... */ construct. They can span multiple lines. Line 1, Char 1
XML documentation
Comments of the form
/// are used to create documentation directly in code. Such comments are usually applied to describe classes, methods, and their parameters. This is especially useful for auto-generating documentation. Line 1, Char 1
Exercises
- Run the code and look at the result. Then comment out the second line of code using a single-line comment, run the code again, and compare the results.
Line 1, Char 1
- Using a multi-line comment, comment out the second and third lines. Run the code and compare the results.
Line 1, Char 1
- Think about when it is more convenient to use single-line comments and when multi-line comments are better.