Comments in C#: Single Line, Multiline and XML Comments

Comments in C#

Introduction: Comments in C#

Comments are used in a program to make a section of code easier to understand. They’re used to make the code more readable and to provide a formal description of the code and how it works. The compiler completely ignores comments, so you may put them wherever in a program and not worry about them breaking it.

You can have three different types of comments in C#.

  • Single Line Comments ( // )
  • Multi Line Comments (/* */)
  • XML Comments ( /// )

Single Line Comments

Single Line comment is preceded by a double forward-slash (//). The code after the double forward slash will be ignored by the compiler and will not be executed.
These comments can be written in a different line or in the same line as the codes.

 // This is an example of single line comment
 Console.WriteLine("Hello World!");

Multi-Line Comments in C#

Multiline Comments are used to comment on multiple lines at once. This is commonly used to comment out an entire section of code.
Multi-line comments begin with /* and end with */.
Any text between /* and */ will be ignored by the C# compiler.

* FileName: Program.cs
* Author:  Shekh Ali
* This is the end of multi line comment 

XML documentation comment

XML documentation comments begin with a triple slash, ///. Comments are enclosed in XML tags. The .NET C# compiler includes a feature that reads the XML documentation comments and creates XML documentation from them. This XML documentation can be extracted to a separate XML file. The XML file can then be styled with XML style sheets to provide elegant code documentation that can be seen in a browser. (Please refer this MSDN link for more information: XML documentation comment)

using System;
namespace ExampleOfXMLComment
    class Program
        /// <summary>
        /// Author: Shekh Ali
        /// Method to print 'Hello World' in the Console application.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
            Console.WriteLine("Hello World!");

Note : In the above example, the <summary> tag provides the information about the defined type or member, whereas the <param> tag represents method parameters.

Note: we can use Ctrl+K+C and Ctrl+K+U to Comment or Uncomment selected lines in C#.


In this post, You’ve learned about the different types of comments in C#, how to use them, and, most importantly, why to use them.
We looked at single-line, multi-line and xml comments in C#.
Code comments can help you understand your code better, as well as other programmers who will use it.

Q: When should you use a single-line or multi-line comment?

Ans: It is entirely up to you which approach you wish to employ. We usually use single-line // comments for short comments and multi-line /* */ comments for longer ones.

Q: What are comments in C#?

Ans: Comments are used in a program to help the reader understand a portion of code. They’re used to make the code more readable as well as provide a formal description of how the code works.

Q: What are the various types of comments in C#?

Ans: There are three different types of comments in C#: single line, multi-line, and XML documentation comments.

Q: How do you comment multiple lines in C#?

Ans: Multi-line comments begin with /* and ends with */ . The  compiler in C# will ignore any text between / *and */.

Q: What are some of the benefits of XML comments?

Ans: XML comments help speed up development by providing intellisense to custom methods and other code structures. XML comments facilitate code reuse by providing a path for generating API-style reference documents from source code.
XML documentation comments in C# begin with a triple slash, ///. Comments are enclosed in XML tags.

Articles to Check Out:

5 1 vote
Article Rating
Notify of

Inline Feedbacks
View all comments