Comments in C#



A comment is just a text that you can write to explain or describe your code.

There are two ways we can write cmments in C# - Multi line comments and single line comments

The multi-line comments begin with the characters "/*" and end with the characters "*/" as following:

/* this code calculates the
* area of a rectangle */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
	
	    int length;
            int breadth;
            int area;

            length = 20;
            breadth = 10;



            area = length * breadth;

		/* this code calculates the 
		 area of a rectangle */
		
	    Console.WriteLine("Length of Rectangle is : {0}", length);	
	    Console.WriteLine("Breadth of Rectangle is : {0}", breadth);	
            Console.WriteLine("Area of Rectangle is : {0}", area);
            Console.ReadLine();


        }
    }
}

If you run the above program you should be able to see an output something similar to

	
Length of Rectangle is 20
Breadth of Rectangle is 10
Area of rectangle is 200



Commenting is often used as debugging aid. We can comment out a section of code to see what result it gives. For example take a look at the code ...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
	
	    int length;
            int breadth;
            int area;

            length = 20;
            breadth = 10;



            area = length * breadth;

		/* this code calculates the 
		 area of a rectangle 
		
	    Console.WriteLine("Length of Rectangle is : {0}", length);	
	    Console.WriteLine("Breadth of Rectangle is : {0}", breadth);	

		*/
            Console.WriteLine("Area of Rectangle is : {0}", area);
            Console.ReadLine();


        }
    }
}



We have commented out a section of code displaying the length and breadth of the rectangle. This gives the following output.


Area of rectangle is 200



The single-line comments begin with the characters "//" and you can write your comments only at this line as the following: // This is a single line comment If you want to write more than one line in your comment then you must begin each line with "//" characters as following: // This code is very complex to undestand // Refer to documentation at www.referencedesigner.com for details


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
	
	   int length;
           int breadth;
           int area;

           length = 20;
           breadth = 10;

           area = length * breadth;// Calculate the area of rectangle

            Console.WriteLine("Area of Rectangle is : {0}", area); 
            Console.ReadLine();

	// The program ends here. 
	// Now save it and hit F5 to run the program			


        }
    }
}


This completes your understanding of the comments in C#. In the next chapter we will show you how to display the values of variables and string using the Writeline command. These comment and Writeline form the two building blocks of C# that you will use to experiment various example codes of C#