C# Comments
Comments are part of the program that are not executed, but increase the readability of the program.
You can write comments in C# in two ways based on the number of lines. They are:
- Single Line Comments
- Multi-line Comments
Single Line Comments
Two forward slashes //
are used to define a single line comment. Anything that comes after double slash //
is considered a comment. A single line comment alone can exist in a single line or it can come after a statement.
Program.cs
using System; namespace CSharpExamples { class Program { static void Main(string[] args) { //this is a single line comment Console.WriteLine("Hello TutorialKart"); //this is also a comment } } }
Output
Hello TutorialKart
If a program statement is commented, it will not be executed.
Program.cs
using System; namespace CSharpExamples { class Program { static void Main(string[] args) { //Console.WriteLine("Hello World"); Console.WriteLine("Hello TutorialKart"); } } }
Output
Hello TutorialKart
The first Console.WriteLine statement is commented, hence not executed.
Multi-line Comments
Multi-line Comments can span across one or more lines. /* your multiline comment */
is the syntax of a multi-line comment.
Program.cs
using System; namespace CSharpExamples { class Program { static void Main(string[] args) { /* This is a multi-line comment */ Console.WriteLine("Hello TutorialKart"); } } }
Output
Hello TutorialKart
You can comment a set of statements using multi-line comment.
Program.cs
using System; namespace CSharpExamples { class Program { static void Main(string[] args) { Console.WriteLine("Hello TutorialKart"); /* Console.WriteLine("Hello World"); Console.WriteLine("Hello User"); */ } } }
Output
Hello TutorialKart
Conclusion
In this C# Tutorial, we learned how to write single line and multi line comments in C# and how to use them in different scenarios.