Topic :Comments in C# Programming: Single Line Comments | Multiline Comments

Comments in C#:

Comments are portions of the code ignored by the compiler which allow the user to make simple Notes in the relevant areas of the source code. Comments come either in block from or as single lines.



Single line Comments:

Single line Comment start with //(double slash) symbol which tell the compiler to ignore everything to the end of the line.



Example:
using System;


namespace firstProject
{
    class Program
    {
        static void Main(string[] args)
        {

            //This is the single Line Comment:
            //this is Another Single line Comment:

            Console.WriteLine("Welcome to Csharp Tutorials.");
            Console.ReadKey();
        }
    }
}


Typically, the single line comment is used to make a Quick Comment about a Single line of Code.



Multi-line Comments:

Multi-line Comment start with /* and end with */ which tell the compiler to ignore everything between these two symbols.



Example:
using System;


namespace firstProject
{
    class Program
    {
        static void Main(string[] args)
        {

           /* This is 
            The multiline 
            Comment */

            Console.WriteLine("Welcome to Csharp Tutorials.");
            Console.ReadKey();
        }
    }
}


Since everything between the symbols is ignored, you will sometimes see Programmers use multi-line comment to beautify their code.



Proper Use of Comments:

Comment should be used for libraries, Function, or class, and can describe what These program can do. Comment should be logical, which can give the reader a good idea of what the Program is trying to Accomplish without having to look at the actual code. The user(Possibly someone else, or you if you're trying to reuse code (you're already previously written) can tell at a glance whether the code is relevant to what he or she is trying to accomplish. This is Particularly important when working as part of a team, where not everybody will be familiar with all of the code.

At the statement level, Comments should be used to describe why the code is doing something. A bad statement comment explains what the code is doing. if you ever write code that is so complex that needs a comment to explain what a statement is doing, you Probably need to rewrite your statement, not comment it.



Bad way of Comments:


using System;


namespace firstProject
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)        //This is For Loop: 
            {
                Console.WriteLine("value of 1: {0}", i);
            }

            Console.ReadKey();
        }
    }
}

the comment (this is for loop) in loop statement doesn't make any sense. because we already know that its for loop. so don't need to put comment here. so its an bad Programming practice to write comments like this way.



Good way of Comments:


using System;


namespace firstProject
{
    class Program
    {
        static void Main(string[] args)
        {
            //This Loop will print the Value of "i" from 0 to 9.
            for (int i = 0; i < 10; i++)        
            {
                Console.WriteLine("value of 1: {0}", i);
            }

            Console.ReadKey();
        }
    }
}

But here, this comment can make sense. because it shows the function of that loop. so its a good Programming practice to write comments like this way.



Commenting is the art of describing what your Program is going to do. so its the good Programming Practice to put comments in your Program. so anyone can understand your code. and after a long time if you need to reuse you written code, then your comments will help you to understand the code. if you didn't put any code then it will be very difficult to understand the code and reuse it.















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement