Click here to Skip to main content
Click here to Skip to main content
Go to top

Preprocessor Directives - Design Practice

, 14 Jan 2014
Rate this:
Please Sign up or sign in to vote.
What is a Preprocessor Directive?A preprocessor directive is a piece of code that is meant explicitly for the compiler.  This offers a programmer

Editorial Note

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

What is a Preprocessor Directive?

A preprocessor directive is a piece of code that is meant explicitly for the compiler.  This offers a programmer to focus compilation for a specific environment at compile time instead of runtime.

How do i identify a Preprocessor Directive?

C#

In C# preprocessor directives can be identified by a hash (#) infront of a word or statement. C# Preprocessor Directives [only bolded directives are covered in this wiki]:

VB.NET

In VB preprocessor directives can be identified by a hash (#) in front of a word or statement. VB.NET Preprocessor Directives [only bolded directives are covered in this wiki]:

What's the advantage of using Preprocessor Directives?

Depending on the Preprocessor Directive there are many advantages to using directives over in code conditional statements.

C# - Conditional Directives 

Pairing the #define, #if, #else, and #endif Conditional statements allows you to set variables to target your Testing vs. Production environments.

 Figure 1 [C#]

VB.NET - Conditional Directives

Pairing the #Const, #If...Then. #else, and #End If Conditional statements allow you to set variables to target your Testing vs. Production environments.

Figure 1 [VB]

1.a

This is how you declare a preprocessor directive. I've defined a TEST variable to be able to switch between my Test environment and Production environment.

1.b

Using the conditional directive, I check to see if TEST is defined, seeing how it is currently defined the constant _toEmail gets set to 'test@company.com'. However if i was to comment out the #define TEST, the _toEmail constant would be set to 'user@ClientCompany.com'.

1.c

You can also use the not (!) comparison operator to declare that code is only run if TEST is undefined.  As you'll see here i have wrapped my preprocessor directive around my try catch block this way i will be able to get an understanding of what types of errors will be thrown without the try catch there.  This will allow me to catch the appropriate Exceptions, as seen in 1.e

1.d

This scenerio is just to show that member variables can also be changed by a preprocessor condition.  This can come in handy if you pair the condition to change the subject to match a rule you have set up in outlook for test email filtering.

1.e

Now that i've run through some testing and have seen the Exceptions that my program is throwing i can now catch the appropriate Exceptions and handle them.  Once you have a good idea of what exceptions you need to handle you can remove the preprocessor condition and the try catch block can be tested to show the appropriate user friendly messages.

Summary

Hopefully now that you have seen an example you can understand some of the advantages of using preprocessor directives. If you would have made an in code switch in 1.b, you would have had to declare an extra boolean, and the conditional statement would be taking up valuable runtime processing time, memory for the (unnecessary) variable.  When using the preprocessing directives in Visual Studio, you will notice that there is a visual queue as to what variable is currently active. This makes your application more readable, for you and future developers working on your code, as well as saving your countless read throughs of your code to understand what version of your code is running.

C# - Organizational Directives

Using the #region and #endregion directives you can outline sections of your code and logically organize them within a descriptive code region.

Figure 2 [C#]

VB.NET - Organizational Directives

Using #Region and #End Region  directives you can outline sections of your code and logically organize them within a descriptive code region.

Figure 2 [VB.NET]

Summary

Hopefully you can see the usefulness of #regions (#Regions in VB.NET).  Not only can you label sections of code, but in the Visual Studio IDE these tags allow you to collapse the code contained in the directive to make your code smaller and easier to read, maintain and manage.

Summary

Hopefully over the course of this article you have seen some of the benefits of using Preprocessor Directives.  Remember: Preprocessor Directives are evaluated at Compile time eleminating bottle necks of conditional statements in your code to switch between Testing and Production code are eliminated freeing up resources for your actual application. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community

United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
 
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

247 members


Comments and Discussions

 
GeneralUsing PinmemberPIEBALDconsult29-Jul-14 17:35 
Questionimages are not displayed PinmemberSouthmountain14-Jan-14 11:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 14 Jan 2014
Article Copyright 2008 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid