Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Nov 9, 2022, 1:35 AM EST
There are 3 types of comments in C#, There are
1) Single line comments:
// for single line comments2) Multiple line comments
/* for multi line comments */3) XML tags comments
/// XML tags displayed in a code commenthttps://www.c-sharpcorner.com/uploadfile/puranindia/comments-in-C-Sharp/
Mukesh NailwalRank #793 · 2.9K reputationPosted Nov 9, 2022, 1:46 AM EST
3 types of comments are in C#:
//)/* */)///)Example Single Line comments:
// 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!"); } } }Example Multi Line comments:
/* 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!"); } } }Example XML comments:
/// /// This is a hello world program. /// using System; namespace HelloWorld { class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }Vishal YelveRank #178 · 13.6K reputationPosted Nov 9, 2022, 1:33 AM EST
In C#, there are 3 types of comments:
Single Line Comments ( // )
Multi Line Comments ( /* */ )
XML Comments ( /// )
Uday DodiyaRank #239 · 10.3K reputationPosted Nov 9, 2022, 1:34 AM EST
Comments are used in a program to help us understand a piece of code. They are human readable words intended to make the code readable. Comments are completely ignored by the compiler.
In C#, there are 3 types of comments:
//)/* */)///)Single Line Comments
Single line comments start with a double slash
//. The compiler ignores everything after//to the end of the line. For example,Here,
Adding 5 and 7is 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:
and
Single line comments can be written in a separate line or along with the codes in same line. However, it is recommended to use comments in a separate line.
Multi Line Comments
Multi line comments start with
/*and ends with*/. Multi line comments can span over multiple 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:
and
XML Documentation Comments
XML documentation comment is a special feature in C#. It starts with a triple slash
///and is used to categorically describe a piece of code.. This is done using XML tags within a comment. These comments are then, used to create a separate XML documentation file.If you are not familiar with XML, see What is XML?
Example 3: Using XML documentation comment
/// /// This is a hello world program. /// 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
The XML documentation (.xml file) generated will contain:
Visit XML Documentation Comments if you are interested in learning more.
Use Comments the Right Way
Comments are used to explain parts of code but they should not be overused .
For example:
Using comment in the above example is not necessary. It is obvious that the line will print Hello World. Comments should be avoided in such cases.
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 9, 2022, 4:52 AM EST
In C#, there are 3 types of comments:
//)/* */)///)