Comments are notes that programmers insert into source code. Comments are ignored by the compiler and therefore will not be executed as part of the program.
Comments are often used by programmers to explain things in human language, such as what a piece of source code is suppose to do for example. This is most helpful when either returning to a piece of code that the programmer has forgotten about, and/or when a new programmer acquires the code and is now trying to figure out what that code is suppose to do.
Comments also allow programmers to leave other various notes within their code, either directed to themselves and/or to other programmers, such as TODO notes, and notes that states the date and time that some function was last modified and by whom (information that is most helpful to programmers, especially when working in teams).
Since comments are ignored by the compiler, and are used by programmers for programmers, then it is safe for you to have and use as many comments within your source code as you desire.
Although some experienced programmers frown on using comments, I myself am a BIG FAN of comments. I use comments in my code all the time, mainly to help remind myself what I was thinking when I wrote the code in the first place.
There are two ways that you can make or leave comments within C# source code:
/* ... */
Single-Line comments start with two forward slashes (
//), and extend all the way to the end-of-the-line. Any text positioned between the
// and the end-of-the-line is considered a comment, and will be ignored by C# compiler (it will not be executed).
The following is an example of a single-line comment, which is placed before a line of code. In this example the comment is used to explain what the line of code is suppose to do:
// Output "Hello World" to the console window:Console. WriteLine( "Hello World!");
Even though it is still considered a Single-Line Comment, it is possible for you to use multiple Single-Line Comments repeatedly if you so desire:
// This is a test comment. // May 21, 2023 // Output "Hello World" to the console window:Console. WriteLine( "Hello World!");
Multi-Line Comments start with
/* and end with
/*, and anything in-between them is considered to be a comment.
The following is the aboves example, rewritten to use multi-line comment instead of mutlple single-line comments:
/* This is a test comment. May 21, 2023 Output "Hello World" to the console window:*/Console. WriteLine( "Hello World!");
It can also be written like follows:
/* This is a test comment. May 21, 2023 Output "Hello World" to the console window: */Console. WriteLine( "Hello World!");
The point is, with Multi-Line Comments, anything between the
*/ is considered to be a comment, and will be ignored by the compiler.
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.