In this tutorial, we will learn about C# comments, different style of comments, and why and how to use them in a program.

Comments can be used to clarify C# code, and to make it more readable. It can likewise be used to prevent execution when testing alternative code.

Comments are used in a program to assist us with understanding a piece of code. They are human readable words planned to make the code readable. Comments are totally disregarded by the compiler.

In C#, there are 3 types of comments:

  1. Single Line Comments (/)
  2. Multi Line Comments (/*/)
  3. XML Comments (/)

Single Line Comments

Single line comments start with a double slash/. The compiler overlooks everything after//to the end of the line. For instance,

int a = 5 + 7; // Adding 5 and 7

Here, Adding 5 and 7 is the comment.

Example 1: Using single line comment

// Hello World Program
using System;

namespace HelloWorld
{
	class Program
	{
		public static void Main(string[] args)  // Execution Starts from Main method
		{
			// Prints Hello World
			Console.WriteLine("Hello World!");
		}
	}
}

The above program contains 3 single line comments:

// Hello World Program
// Execution Starts from Main method

What’s more,

// Prints Hello World

Single line comments can be written in a different line or alongside the codes in same line. Nonetheless, it is recommended to use comments in a different line.


Multi Line Comments

Multi line comments start with/* and ends with */. Multi line comments can length over various lines.

Example 2: Using multi line comment

/*
	This is a Hello World Program in C#.
	This program prints Hello World.
*/
using System;

namespace HelloWorld
{
	class Program
	{
		public static void Main(string[] args)
		{
			/* Prints Hello World     */
			Console.WriteLine("Hello World!");
		}
	}
}

The above program contains 2 multi line comments:

/*
This is a Hello World Program in C#.
This program prints Hello World.
*/

What’s more,

/* Prints Hello World */

Here, we may have seen that it isn’t compulsory for a multi line comment to length over different lines./* … */can be used rather than single line comments.


XML Documentation Comments

XML documentation comment is a special feature in C#. It begins with a triple slash///and is used to categorically describe a piece of code.. This is finished using XML tags inside a comment. These comments are at that point, used to create a different XML documentation file.

Example 3: Using XML documentation comment

/// <summary>
///  This is a hello world program.
/// </summary>

using System;

namespace HelloWorld
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Hello World!");
		}
	}
}

The XML comment used in the above program is

/// <summary>
/// This is a hello world program.
/// </summary>

The XML documentation (.xml file) generated will contain:

<?xml version="1.0"?>
<doc>
	<assembly>
		<name>HelloWorld</name>
	</assembly>
	<members>
	</members>
</doc>

Visit XML Documentation Comments on the off chance that you are interested in learning more.


Use Comments the Right Way

Comments are used to clarify portions of code however they ought not be overused.

For instance:

// Prints Hello World
Console.WriteLine("Hello World");

Using comment in the above example isn’t required. Clearly the line will print Hello World. Comments ought to be stayed away from in such cases.

• Instead comments ought to be used in the program to clarify complex algorithms and methods.

• Comments ought to be short and forthright rather than a long description.

• As a rule of thumb, it is smarter to clarify why rather than how, using comments.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder