C# Comments (Single Line, Multiline, XML)

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

 

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

 

In c#, we can include the comments anywhere in the program without effecting our code and the comments in c# does not affect the performance of an application because the comments won’t be compiled and executed by the compiler.

 

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

 

  • Single Line Comments
  • Multiline 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 c# programming language.

 

// Single Line Comment

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

 

Following is the example of defining the single line comments in 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 console

        }

        // This Method will display the welcome message

        public static void GreetMessage()

        {

            Console.WriteLine("Welcome to Tutlane");

        }

    }

}

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

 

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

 

C# Single Line Comments Example Result

 

This is how we can use single line comments to provide a detailed information about the functionality of our c# application.

C# Multiline comments

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

 

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

 

/* Multi Line Comment */

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

 

Following is the example of defining the multiline comments in 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 Any Key to Exit..");

            Console.ReadLine();

        }

        public static void GreetMessage(string message)

        {

            Console.WriteLine(message);

        }

    }

}

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

 

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

 

C# Multiline Comments Example Result

 

This is how we can use multiline comments to provide a detailed information about the functionality of our c# application.

C# XML Comments

In c#, XML Comments are the 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 c# programming language.

 

///<summary>

/// This class does something.

///</summary>

public class SomeClass

{

 

}

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

 

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

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string msg = "Welcome to Tutlane";

            GreetMessage(msg);

            Console.WriteLine("Press Any 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 above code, we defined an xml comments to describe what the specific block or line of code can do 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 above c# program, we will get the result like as shown below.

 

C# XML Comments Example Result

 

This is how we can use xml comments to provide a detailed information about the functionality of our c# application.