Almost any enterprise program is written with over thousands of lines of code! Reading all these codes just to know how the program is written and what the logic behind it is, can be a nightmare. Even the developers of the program can’t read the source code and figure out what they’ve done before! Because let’s face it, we as human tend to forget no-matter how strong our minds might be.
This is why we use comments in source codes.
We use comments in source codes to explain the purpose and its details. This will help everyone to understand how the program works and they can read and enhance the program easier in later time if needed.
In C# programming we can put comments in places like:
-
Above or below a method or class.
-
Above or below a variable declaration, initialization and
assignments. -
Generally speaking, if there’s a white space in your source code,
there’s a good chance that you can add comments in there too!
Let’s take a look at an example:
using System;
namespace quickExample
{
//The name of the class is Program
public class Program
{
//The class has only one method and that is ‘Main’
public/*comments between access specifier and the static keyword*/ static void Main(/* The method takes one argument and that is and array of strings*/string[] args)
{
/*A variable of type String is created*/
string/*comments between data-type and the variable*/ message = "Hello World";
//The value of the variable ‘message’ is sent to the output stream.
Console.WriteLine(message);
//End of the method
}
//End of the class
}
}
Output: Hello World
If you run the program above, the code will run without any problem even though we have mixed it with comments.
There are two types of comments in C# programs:
-
Single line commenting: This type of comment starts with two
forward-slashes `//` and after that we can put comments.
Note: As the name suggest, this type of commenting is single line and if we need another line for comment, we need to start with two forward slashes // again.
Also in single line comment, we can’t put any actual source code and expect to be compiled by the compiler because compilers simply ignore comments no-matter what is in that comment.
Example:
// this is a single line comment.
-
Multiple lines commenting: this type of commenting is not limited
to just one line and we can put comments on multiple lines that follow
each other.
Here’s the structure of a multiline comment:
/* body of the comment */
A multiline comment starts with forward-slash followed by an asterisk followed by the actual comment which can be in one or multiple lines and then followed by another asterisk and ends with another forward-slash.
Remember: there’s no space between the forward-slash / and the asterisk * that comes before and after the comments.
Note: we can’t use comments between the names of C# keywords, variables and methods.
Example:
ret/*a comment between the 'return' keyword! */urn 0;
This is illegal and the compiler will return error because the return is a C# keyword and we broke it by adding comments in between.
Note: at the end of this section we should remind you that just because you’re able to put comments in many areas in your source code, that doesn’t mean you should! Comments are for situations and areas in the source code that we can’t read and make sense of that code. But if we fill the entire source code with unnecessary comments, it just makes the source code more confusing instead of helping!!!