Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to write multi-line comments in C#?
Multi-line comments in C# are comments that span more than one line. They are useful for adding detailed explanations, documenting code blocks, or temporarily disabling large sections of code during development.
Syntax
Multi-line comments in C# use the following syntax −
/* This is a multi-line comment that can span multiple lines All text between /* and */ is ignored */
The compiler ignores everything between /* and */, making it perfect for lengthy explanations or code documentation.
Basic Multi-line Comment Example
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
/* This is a multi-line comment
explaining the purpose of this program.
It demonstrates how multi-line comments work in C# */
// Single line comment for comparison
Console.WriteLine("Hello World");
}
}
}
The output of the above code is −
Hello World
Using Multi-line Comments for Code Documentation
using System;
class Calculator {
/*
* This method calculates the area of a rectangle
* Parameters: length and width (both double values)
* Returns: area as a double value
* Formula: area = length × width
*/
public static double CalculateArea(double length, double width) {
return length * width;
}
static void Main(string[] args) {
double result = CalculateArea(5.0, 3.0);
Console.WriteLine("Area of rectangle: " + result);
}
}
The output of the above code is −
Area of rectangle: 15
Using Multi-line Comments to Disable Code
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("This line will execute");
/*
Console.WriteLine("This line is commented out");
Console.WriteLine("This line is also commented out");
int x = 10;
Console.WriteLine("x = " + x);
*/
Console.WriteLine("This line will also execute");
}
}
The output of the above code is −
This line will execute This line will also execute
Key Rules for Multi-line Comments
-
Multi-line comments start with
/*and end with*/. -
Everything between these delimiters is ignored by the compiler.
-
Multi-line comments cannot be nested ? you cannot put one multi-line comment inside another.
-
They can span any number of lines and can appear anywhere in your code.
Comparison of Comment Types
| Comment Type | Syntax | Use Case |
|---|---|---|
| Single-line | // comment |
Brief explanations, inline notes |
| Multi-line | /* comment */ |
Detailed documentation, disabling code blocks |
| XML Documentation | /// <summary> |
API documentation, IntelliSense support |
Conclusion
Multi-line comments in C# use the /* */ syntax and are essential for documenting complex code sections or temporarily disabling code blocks. They provide a clean way to add detailed explanations that span multiple lines without cluttering your code with numerous single-line comment markers.
