C# Comments (Single Line, Multiline, XML)

In c#, Comments are self-explanatory notes to provide detailed information about the code which we wrote in our applications.

 

It’s always a good practice to include comments in our c# code to provide detailed information about the functionality, like what a specific block or line of code can do. It will benefit anyone else who examines the code.

 

In c#, we can include the comments anywhere in the program without affecting our code. The comments in c# do not affect an application's performance because the comments won’t be compiled and executed by the compiler.

 

In c#, there are three types of comments available, those are

 

  • Single-line Comments
  • Multi-line Comments
  • XML Comments

C# Single Line Comments

In c#, Single Line Comments are defined by using // (double forward slash).

 

Following is the syntax of defining the single-line comments in the c# programming language.

 

// Single Line Comment

Now we will see how to define a single-line comment in c# programs with examples.

 

Following is the example of defining the single-line comments in the c# programming language.

 

using System;

namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
              // Calling Method to Show Greet Messaging
              GreetMessage();
              Console.WriteLine("Press Any Key to Exit..");
              Console.ReadLine(); // This method to read the commands from a console
         }
         // This Method will display the welcome message
         public static void GreetMessage()
         {
             Console.WriteLine("Welcome to Tutlane");
         }
     }
}

If you observe the above code, we defined a single-line comment to describe the specific block or line of code in our c# application.

 

When we execute the above c# program, we will get the result below.

 

C# Single Line Comments Example Result

 

This is how we can use single-line comments to provide detailed information about our c# application's functionality.

C# Multiline comments

In c#, Multiline Comments are used to comment the multiple lines of code, and the multiple lines of comments are surrounded by slash and asterisk like /* …… */.

 

Following is the syntax of defining the multiline comments in the c# programming language.

 

/* Multi Line Comment */

Now we will see how to define multiline comments in c# programs with examples.

 

Following is the example of defining the multiline comments in the c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
             /*
              Define & assign a variable and
              Send a variable to the method to display
             */
             string msg = "Welcome to Tutlane";
             GreetMessage(msg);
             Console.WriteLine("Press Enter Key to Exit..");
             Console.ReadLine();
        }
        public static void GreetMessage(string message)
        {
            Console.WriteLine(message);
        }
    }
}

If you observe the above code, we defined multiline comments to describe the specific block or line of code in our c# application.

 

When we execute the above c# program, we will get the result below.

 

C# Multiline Comments Example Result

 

This is how we can use multiline comments to provide detailed information about our c# application's functionality.

C# XML Comments

In c#, XML Comments are a special type of comments, and these will be added above the definition of any user-defined type or member.

 

In c#, the XML Comments are defined by using /// (triple forward slashes) and with XML formatted comment body.

 

Following is the syntax of defining the XML comments in the c# programming language.

 

///<summary>
/// This class does something.
///</summary>

public class SomeClass
{

}

Now, we will see how to define XML comments in c# programs with examples.

 

Following is the example of defining XML comments in the c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "Welcome to Tutlane";
            GreetMessage(msg);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
        ///<summary>
        /// Method to Display Welcome Message
        ///</summary>
        ///<param name="message"></param>
        public static void GreetMessage(string message)
        {
           Console.WriteLine(message);
        }
    }
}

If you observe the above code, we defined XML comments to describe the specific block or line of code in our c# application.

 

Here the <summary> tag is used to add detailed information about a type or member and <param> tag is used to describe the method parameters.

 

When we execute the above c# program, we will get the result below.

 

C# XML Comments Example Result

 

This is how we can use XML comments to provide detailed information about our c# application's functionality.