Web Development & Execution
David Addison
by David Addison
share this
?fl
« Back to the Blog

C# conditional compilation symbols

11/01/2010
C# conditional compilation symbols

I have known about conditional compilation symbols for a long time. However, I have never had a use for them, until now. Today we were working on porting part of our Dirigo AdxStudio CMS forms library to a client Web site. The Dirigo AdxStudio CMS forms library is built atop a standardized Dirigo forms component library. The assemblies are: Dirigo and Dirigo.Adx.

Since some of the code used in Dirigo.Adx references objects in Dirigo, the question was how to arrive at an easy-to-manage solution while maintaining code portability. It would have been easy to just comment out the parts of the code that reference the Dirigo code library but using conditional compilation symbols is a more formal approach.

To sum it up, conditional compilation symbols are if statements that are interpreted by the compiler at compile time. They let you conditionally compile portions of code. This is handy in the above example. We basically define a boolean value and use #if statements to tell the compiler what to compile or not compile. Here is a short example:

#define JustTesting // first line
.....
namespace Dirigo
{
public static class TestCompilationSymbols
{
#if JustTesting
public static void DisplayMessage()
{
HttpContext.Current.Response.Write("JustTesting is defined and therefore true.");
}
#elif NotJustTesting
public static void DisplayMessage()
{
HttpContext.Current.Response.Write("NotJustTesting is true, JustTesting is false.");
}
#else
public static void DisplayMessage()
{
HttpContext.Current.Response.Write("Really boring default message.");
}
#endif
}
}

Above you can see that we defined a compilation symbol named JustTesting. You do not assign values to compilation symbols in C#, they are boolean only. By simply defininig it, it is set to true. In the code blocks you can see that the last two versions of DisplayMessage() are grayed out, therefore they will not be compiled.

You may be saying, "that's great, but I don't do heavy coding relating to multiple code libraries... why would I want to use this?" Here are some suggested uses:

  • Write localized code, e.g. you could write a French version of a method, then a German, followed by English. Please note this should be used with caution and that you should also review other localization options such as resource files. Most of your localization will probably not be done this way.
  • Track debug code. Every time your code runs, you could setup a sophisticated debug and trace reports to be stored elsewhere. The only problem is that you don't want to have to comment out your debug code every time you compile for release. Just use the built in DEBUG symbol (see link below for more info) and all of your debug code will be omitted from compilation when you build for release.

Want to learn more? There is not too much to it, simple boolean values and if, elif, else, define, and undef (undefine). Definitely search around and make sure to check out this article on defining compilation symbols at the project level so that you don't have to put #define statements at the top of all your pages.

Like Easter Eggs?

We do too.

UpUpDownDownLeftRightBA

Thanks!

Thank you for contacting us!

Our group will review and follow up within 72 hours.
Thanks for your interest!

Back Home ×