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 do we use a #line directive in C#?
The #line directive in C# allows you to modify the compiler's line number and optionally the file name that appear in error and warning messages. This is particularly useful for code generators and preprocessors that need to map generated code back to the original source files.
Syntax
Following is the syntax for the #line directive −
#line number #line number "filename" #line default #line hidden
Parameters
-
number − The line number you want to assign to the following line
-
filename − Optional filename that will appear in compiler output
-
default − Returns line numbering to its original numbering
-
hidden − Hides the following lines from the debugger
Using #line with Custom Line Numbers
You can set a custom line number and filename for error reporting −
using System;
class Program {
static void Main() {
Console.WriteLine("Program starts");
#line 100 "CustomFile.cs"
int unusedVariable1; // Warning on line 100 of CustomFile.cs
int unusedVariable2; // Warning on line 101 of CustomFile.cs
int unusedVariable3; // Warning on line 102 of CustomFile.cs
#line default
int normalVariable; // Warning with normal line numbering
Console.WriteLine("Program ends");
}
}
When compiled with warnings enabled, the output shows custom line numbers and filename for the middle section −
Program starts Program ends
Using #line default
The #line default directive returns line numbering to its original state −
using System;
class LineDirectiveExample {
static void Main() {
Console.WriteLine("Line numbering example");
#line 500
int x; // This will be reported as line 500
int y; // This will be reported as line 501
#line default
int z; // Back to normal line numbering
Console.WriteLine("Example completed");
}
}
The output of the above code is −
Line numbering example Example completed
Using #line hidden
The #line hidden directive hides subsequent lines from the debugger until the next #line directive −
using System;
class HiddenLineExample {
static void Main() {
Console.WriteLine("Visible in debugger");
#line hidden
// These lines are hidden from debugger
int hiddenVar = 10;
Console.WriteLine("Hidden from debugger: " + hiddenVar);
#line default
Console.WriteLine("Visible in debugger again");
}
}
The output of the above code is −
Visible in debugger Hidden from debugger: 10 Visible in debugger again
Common Use Cases
-
Code Generators − Map generated code errors back to original template files
-
Preprocessors − Maintain accurate line numbers after code transformation
-
Template Engines − Show errors in the context of the original template
-
Debug Control − Hide generated or utility code from debugger stepping
Comparison of #line Directives
| Directive | Purpose | Effect on Errors |
|---|---|---|
#line number |
Set custom line number | Errors show custom line number |
#line number "file" |
Set line number and filename | Errors show custom line and file |
#line default |
Restore normal numbering | Errors show actual line numbers |
#line hidden |
Hide from debugger | Lines skipped during debugging |
Conclusion
The #line directive in C# provides precise control over how the compiler reports line numbers and filenames in error messages. It's essential for code generation tools and helps maintain accurate error reporting when source code is transformed or generated automatically.
