This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

#line hidden and #line default

The #line directive allows complete control over the perceived line numbers of a project. This is primarily of use to metaprogramming tools that generate code. The directive includes a variant that allows lines to be hidden from source-level debuggers.

#line Directive

The #line directive is used by code-generation tools, also known as metaprogramming tools, that create source code using design skills. It allows the apparent line numbering of generated code to be modified so that it matches the line numbers or references in the source diagrams and design surfaces. This is useful when developing such tools and has been described in an earlier article.

#line hidden

A special variant of the #line directive is #line hidden. When applied to C# code, all lines of source code that follow the directive are marked as hidden. When the code is executing within the Visual Studio debugger, or other source-level debuggers that correctly support it, these lines will automatically be stepped over when encountered. This is ideal for code that has been generated by a metaprogramming tool and is often found within ASP.NET code that has been generated automatically. It allows the generated code to be stepped over whilst the user code is debugged line by line. This directive can also be useful in your own libraries to prevent fully tested but complex code from being debugged unnecessarily.

NB: When lines have been hidden from a debugger using #line hidden, the line numbering itself is unaffected. If an error occurs, the correct line number information will be provided as usual.

#line default

The second variation of the #line directive that we will consider in this article is #line default. This simple directive reverses the effects of any #line directives that appear before it. If line numbering has been adjusted, it is returned to the default values. If lines have been hidden with #line hidden, the following lines are no longer hidden and will be stepped into by the debugger.

Hidden Lines Example

To demonstrate the use of the two #line directives, add the following code to the Main method of a console application and step through it using the Visual Studio debugger. You will find that the for loop between the two #line directives is skipped by the debugger. You will see from the output in the console window that the hidden lines are executed correctly.


#line hidden

for (int i = 1; i <= 10; i++)
    Console.WriteLine("Hidden Line {0}", i);

#line default

Console.WriteLine("Normal Again");
24 March 2010