BlackWaspTM

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+

C# Conditional Preprocessor Directives

The C# Preprocessor directives allow the code of a project to be changed at compile time depending upon whether symbols have, or have not, been defined. This article describes the use of the conditional preprocessor directives.

Preprocessor Directives

Preprocessor directives are special commands inserted into your code that are analysed by the compiler when a project is built. One common use of preprocessor directives is to modify the compiled code by including or excluding sections according to which symbols have been defined. In this article we will demonstrate this using the conditional preprocessor directives.

NB: Preprocessor directives are used at compile time and change the final program. They do not exist at run-time.

#if Directive

The primary conditional preprocessor directive is #if. This is always followed by the name of a symbol that may or may not be defined. You can also use multiple symbols combined with & or | when code should only be included for certain combinations.

The #if directive is followed by the code that you wish to include in the compiled software if the named symbol exists. The end of the optional code is marked by the inclusion of the #endif directive. If the named symbol is undefined, the code is excluded from the final assembly.

For example, imagine that we have a Windows Form program that has professional and standard editions. One of the differences between the two editions may be that a menu is visible only in the professional version. We could define the menu using the form designer and have it hidden by default. In the code, we would want to display the menu when the "ProEdition" symbol is defined. This could be achieved with the following:

#if ProEdition
    ProMenu.Visible = true;
#endif

If the ProEdition symbol is defined, for example by adding "#define ProEdition" as the first line of the file, the code that shows the menu is included in the final program. If the symbol is missing, this line would be excluded.

NB: In this situation you should also include or exclude the code for the functions provided by the menu using #if statements.

#else Directive

As with the standard if statement in C#, it's possible to have an "else" condition with preprocessor directives. To do so, use the #else directive. If the #if statement's predicate evaluates as true, the code between the #if and the #else directives is compiled. If not, the code between #else and #endif is included in the final program. The following is an example of how this can be used. If the ProEdition symbol is defined, the maximum number of projects is one million. If not, only one project can be created.

#if ProEdition
    const int MaxProjects = 1000000;
#else
    const int MaxProjects = 1;
#endif

#elif Directive

Finally, if you have multiple options, you can create an if-else-if ladder containing several conditions, each with associated code. The initial condition is expressed with the #if directive. Subsequent conditions use the #elif directive and the #else statement is optional.

In the example below, the software product now has three versions, each supporting a different number of projects.

#if ProEdition
    const int MaxProjects = 1000000;
#elif StandardEdition
    const int MaxProjects = 1000;
#else
    const int MaxProjects = 1;
#endif
20 July 2011