Posted by: Cirilo Meggiolaro | 12/3/2008

Tip of the day #50 – Preprocessor directives

Preprocessor directives define code that will be interpreted in compile time and may generate or not specific blocks of code or take some actions after that.

Directive #if #endif

If you define a #if #endif directive for example, if the condition is true, the code within the statement will be compiled. Otherwise, that specific block of code will not be added to the assembly.

The following example defines a #if #else #endif directive checking the compile mode of the application and printing different values on the console screen.

static public void Main(string[] Args)
    #if DEBUG
        Console.WriteLine(“Debug Mode”);
        Console.WriteLine(“Release Mode”);

Directives #define #undef

The #define directive allows you to create a symbol (not a constant) that will determine a condition for compilation. You may use the symbol created in conjunction with #if directive to check if the condition is true.

Use #undef to remove the symbol.

Directive #warning

The #warning directive tells the compiler to display a warning in the error list after compiling. You must define a #warning using a string value like the following code snippet:

#warning “This method must not be used!”

Directive #error

Going to the same direction than the #warning directive, the #error directive defines an error block and avoids the application to be compiled. The example below defines an error directive when the compile mode is debug:

    #error “This method must not be used!”

Directive #pragma

The #pragma directive allows special built-in instructions to be passed to the compiler and the directive scope is the file it is defined. Two #pragma directives are available:

  • #pragma #warning: Enable or disable some warnings;
  • #pragma #checksum: It generates checksums for files to help during the debug process. It works only for ASP.NET files.

Directive #line

The #line directive allows you to modify the line numbers during the compilation. The #line directive may be used to hide some lines from the debbuger as well using the keyword hidden after the #line directive. The following code defines a #line directive that hides two lines of code from debbuger.

#line hidden
    Console.WriteLine(“Hidden line.”);
#line default
    Console.WriteLine(“Normal line #2.”);

A different usage for the #line directive is to define a line number that will be forced to run during runtime like the following code snippet describes:

#line 20 //Enforce the line 20 to run
    int a = 20;
#line hidden //The following line will be skipped by the debugger
    int b = 50;
#line default
    int c = 100;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: