Like many other programming languages. It’s good practice to include the 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 later.
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.
There are three types of comments available, these are
- Single-line Comments
- Multi-line Comments
- XML Comments
C# Single Line Comment
The single line comment starts with // (double slash).
Let's see an example of single line comment in C#.
using System; public class Program { public static void Main() { // Let's declare and print variable in C# int x=30; Console.WriteLine(x); } }
C# Multi Line Comment
The C# multi line comment is used to comment multiple lines of code.
It is surrounded by slash and asterisk (/* ….. */).
Let's see an example of multi line comment in C#.
using System; public class Program { public static void Main() { /* Let's declare and print variable in C#. */ int x=30; Console.WriteLine(x); } }
C# XML Comments
In C# an XML Comment is 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.
using System; public class Program { public static void Main() { int x=30; string msg = "maxcsharp"; Console.WriteLine(x); DisplayMessage(msg); } ///<summary> /// Method to Display Welcome Message ///</summary> ///<param name="message"></param> public static void DisplayMessage(string message) { Console.WriteLine(message); } }