Wednesday, December 5, 2018
Home > C# > Comments in C#

C# Comments

Comments in a program are explanatory statements that help the reader to understand source the code. We use comments in a program to explain the program. We write them in a way that help reader to understand the code.
E.g.

// value of x is 10. 
int x = 10;

As we can see that the above comment is just informing the reader that we are setting value rather than helping the reader to understand the code because a reader can see that value of x is 10 by reading this line.
int x = 10;
Have a look at the following comment.
E.g.

// We need to divide items by 2 here because we brought each item in pairs.
cost = items / 2 * storePrice;

When a reader reads the above-written comment, it will know the purpose of dividing by 2 in the above-written statement.

Why Are Comments in C# used?

Comments are used to tell the reader why we wrote that particular line of the code. You can say that the code tells us how a particular problem is solved but comment/s say why you did it. They help the reader understand what program is doing.

Types of C# Comments

Just like C++, we have Two types of comments in c#.

  • Single Line
  • MultiLine or block

Single Line

We write Single line comments by using two forward slashes //. A C# compiler will ignore everything written after these two slashes.
E.g.

             // this is a single line comment.
using System;

namespace first_Program
{
     class Program
     {
          static void Main(string[] args)
          {
               // A single line comment.
               Console.WriteLine("Single Line Comment Example!!!!");
               Console.ReadKey();
          }
     }
}

When the compiler runs the above-written program, it will consider everything written after // in the same line as a comment and will ignore it. Below image confirm it that // A single line comment didn’t appear in the console.

Single Line Comments in C# example

Multiline or block

We write Multiline or block comments within /* */.
/* is the starting point of a multiline comment/s, and */ is the ending point. The C# compiler considers it as a comment Whatever we write inside these.
E.g.

             /*this is 
                  a 
             multiline comment.*/
using System;

namespace first_Program
{
     class Program
     {
          static void Main(string[] args)
          {
                /*this is 
                    a 
               multiline comment 
                  Example.*/
               Console.WriteLine("MultiLine Comment Example!!!!");
               Console.ReadKey();
          }
    }
}

When this code will run compiler will ignore everything written inside /* */ as shown below.

C# comments - MultiLine comments

Comments are tricky to use like:
// this line of code is commented and is ignored by the compiler. // comment inside comment.
The above line of code will not generate any error. Because everything we write after // is a comment.

But what if we write comment/s like:

/* this is the outer block.

         /*this is the inner block.*/

The outer block will end  Here */
the above will generate an error and will not let compiler to run the program.

Note:- Do not write comment/s inside comment/s.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.