In previous article, I mentioned Switch case multiple conditions in C# but in this article, I have mentioned how we can comment a block of code (multi-line comment) in C# or comment a Single-Line.

While you are developing a web or mobile application or any other type of software, commenting is really helpful so other developers can understand your code easily and quickly.

csharp-comments-single-multi-line-block-comments

In C#, there are 3 types of comments:

  • Single Line Comments ( // Single line comment )
  • Multi Line Comments (/* Your Multi-line comment here*/)
  • XML Comments ( /// comments in XML )

Single Line Comments in C#

Single line comments in C#, starts with a double slash //. The compiler ignores everything after // until the end of the line.

Here is an console application example when using Single-line Comments

using System;

namespace HelloWorld
{
    internal class Program
    {
        // main program execution starts here
        static void Main(string[] args)
        {
            //this line writes hello world
            Console.WriteLine("Hello World");

        }
    }
}

Output:

Hello World

Note: If you want to comment in Visual Studio, you can simply select lines of Code and click 'Ctrl+K, Ctrl+C ' to comment while 'Ctrl+K, Ctrl+U ' to uncomment.

Multi-line Comments in C#

Multi-line comments or block comments in C#, allows us to comment a block of code. It is surrounded by slash and asterisk (/* Comment here */).

Let's take a look at an example, considering above console application

using System;

namespace HelloWorld
{
    internal class Program
    {
        // main program execution starts here
        static void Main(string[] args)
        {
            //this line writes hello world
            Console.WriteLine("Hello World");

            /*
             Add two values
             using C#
            */
            int a = 2, b = 3; 
            int c = a + b;

            //print sum of a,b
            Console.WriteLine(c);
        }
    }
}

Output:

Hello World
5

XML Comments

XML Comments is a special type of comments in C#, which starts with triple slash (///) and these are helpful when you are documenting web-api code in C# or basically adding a function details/summary.

Here is an example of using XML comments in C#

using System;

/// <summary>
///  This is a hello world program.
/// </summary>
namespace HelloWorld
{
    internal class Program
    {
        // main program execution starts here
        static void Main(string[] args)
        {
            //this line writes hello world
            Console.WriteLine("Hello World");

        }
    }
}

Output:

Hello World

When to use comments in C#?

While it is good practice to write down comments in your C# code, but for simpler code, like below, we can ignore comments.

           //this line prints hello world
            Console.WriteLine("Hello World");

But it is always good to explain complex functions/methods using Comments.

You may also like to read:

Visual Studio Shortcuts (Comment, Multi-line etc)

Show or Hide Whitespace, Tabs in Visual Studio

Change theme in Visual Studio

Format Code in Visual Studio (With Indentation)

How to define C# multiline string literal?

How to get Currently logged in UserId in ASP.NET Core?

How to get size of an Amazon S3 bucket?

Remove Duplicates from Array in C#

Get Character to ASCII value in C#

SQL Comments (Comment in SQL Query)