Saturday, February 27, 2016

C# Comments

Comments are the information provided to understand what the following code is going to do. It helps in describing the program. However, they are not parsed by the software, or, in other words, it does not affect the final piece of code. Comments are just the piece of information for the understanding of the programmer. It is a good habit to write comments in a program to make it understandable.

Comments are of four types:
  1. Single Line comments
  2. Multi Line comments
  3. Trailing comments
  4. Default comments
Single Line comments
Single line comments are the comments written in one line. The line is started with a double forward slash, (//). The text or code following the double slash is ignored during compilation. We can write any kind of information after this double forward slash may be to provide information or commenting any code if not needed.

Example:
  1. // This is a text comment and the next line is commented out code  
  2. // Console.WriteLine("Hello, World");  
Multiple Line comments
Multiple line comments are comments written in many lines. The comment’s start symbol is (/*) and an end symbol is (*/). Any information which is written in between these two symbols is treated as a comment. It is useful for commenting out large blocks of code or giving large chunk of information.

Example:
  1. /* This is a multi-line comment 
  2. and it will end when the next 
  3. comment symbol is reached */  
Trailing comments

A trailing comment is a kind of single line comment only but differs slightly. Here, the double slash (//)appears at the end of a line for providing the information about the line of code in same line. The code written before the comment symbol is treated as a piece of code from the program. Text written after the symbol is treated as comment.

Example:
  1. Console.WriteLine("Hi"); // Trailing message for the output Hi  
Default comments
Default comments are the comments written by the program itself when you create a new project to work on. The line is started with a triple forward slash, (///). The text or code following the triple slash is ignored during compilation similar to single line comment. The comments after the forward slashes are written as XML tags within the “<>” symbols. The information after these forward slash provide information about what you need to write after this line of comment or any entry point or what next code should contain.

Example:
  1. ///<Summary>  
  2. ///This is the main entry point of the program  

No comments:

Post a Comment