How do we use a #line directive in C#?


It lets you modify the compiler's line number and (optionally) the file name output for errors and warnings.

Let us see some examples.

#line 100 "demo"  
int a;    // CS0168 on line 100  
int b;    // CS0168 on line 101  
int c;  // CS0168 on line 102

As shown above the example reports three warnings associated with line numbers. The #line 100 directive forces the line number to be 100 and until the next #line directive, the filename will be reported as "demo”.

Let’s see another example: The default directive returns the line numbering to its default numbering. This directive then counts the lines that were renumbered by the previous directive.

#line default  
char a;   // CS0168 on line 15
float b;  // CS0168 on line 16 

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements