Author : Rahul
Last Modified : 06-Jan-2021
Complexity : Beginner
Comments in C#
C# provides three types of comments. These are as follows:
- Single-line Comments
- Multi-line Comments
- Documentation Comments
1. Single Line Comments
These types of comments are used to comment on a single line at a time. These comments are used to comment on a single line of code/statement.
Syntax: // Single Line Comment
Example: // C# program for single-line comment using System; class SinglelineComment { // Single Line Comment - Print message public static void PrintMessage(string message) { Console.WriteLine(message); } // Main function static void Main(string[] args) { string message = "Example of Single Line Comment"; // Calling function PrintMessage(message); } }
Output:
Example of Single Line Comment
2. Multiline Comments
These types of comments are used to comment multiple lines at a time. These comments are used to comment a block of code/statement.
Syntax: /* Multiline Comment */
Example: // C# program for multiline comment using System; class MultilineComment { public static void PrintMessage(string message) { Console.WriteLine(message); } static void Main(string[] args) { /* Multiline Comment - Define a string variable and assign string value into it*/ string message = "Example of Multiline Comment"; PrintMessage(message); } }
Output:
Example of Multiline Comment
3. Documentation Comments
These types of comments are used to create the documentation of code by adding XML elements. XML elements are added in Documentation Comments.
Syntax: /// <summary> /// Documentation Comment /// </summary>
Example: // C# program for Documentation Comments using System; class DocumentationComment { /// <summary> /// Method is used to print message on console /// </summary> /// <param name="message">message</param> public static void PrintMessage(string message) { Console.WriteLine(message); } static void Main(string[] args) { string message = "Example of Documentation Comment"; PrintMessage(message); } }
Output:
Example of Documentation Comment