News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

C# Tutorial – 26 – Preprocessor

C# includes a set of preprocessor directives that are mainly used for conditional compilation. Although the C# compiler does not have a separate preprocessor, as C and C++ compilers, the directives shown below are processed as if there was one. That is, they appear to be processed before the actual compilation takes place.

DirectiveDescription
#if
#elif
#else
#endif
If
Else if
Else
End if
#define
#undef
Symbol define
Symbol undefine
#error
#warning
#line
Generate error
Generate warning
Set line number
#region
#endregion
Mark section start
Mark section end

Preprocessor directive syntax

The preprocessor directives are easily distinguished from normal programming code in that they start with a hash sign (#). They must always occupy a line that is separate from anything else, except for single-line comments. Whitespace may optionally be included before and after the hash mark.

#line 1 // set line number

Conditional compilation – #if and #endif

The #if and #endif directives specify a section of code that will be included or excluded based on a given condition. Most often, this condition will be a conditional compilation symbol.

#if MySymbol
 // …
#endif

Defining symbols

A conditional compilation symbol is created using the #define directive followed by the symbol’s name. When a symbol is defined, it will then cause a conditional expression using that condition to be evaluated as true. The symbol will remain defined only within the current source file starting from the line where the symbol is created.

#define MySymbol

Undefining symbols

The #undef (undefine) directive can disable a previously defined symbol.

#undef MySymbol

Conditional compilation – #elif and #else

Just as with the C# if statement, the #if directive can optionally include any number of #elif (else if) directives and one final #else directive. Conditional directives may also be nested within another conditional section. In longer conditionals, it is a good practice to add comments to the #endif directives to help keep track of which #if directive they correspond to.

#if Professional
 // …
#elif Advanced || Enterprise
 // …
#else
  #if Debug
   // …
  #endif // Debug
#endif // not Professional, Advanced or Enterprise

Diagnostic directives

There are two diagnostic directives: #error and #warning. The #error directive is used to abort a compilation by generating a compilation error. This directive can optionally take a parameter that specifies the error description.

#if Professional && Enterprise
 #error Build cannot be both Professional and Enterprise
#endif

Similar to error, the #warning directive generates a compilation warning message. This directive will not stop the compilation.

#if !Professional && !Enterprise
 #warning Build should be Professional or Enterprise
#endif

Line directive

Another directive that affects the compiler’s output is #line. This directive is used to change the line number and optionally the source file name that is displayed when an error or warning occurs during compilation. This is mainly useful when using a program that combines the source files into an intermediate file which is then compiled.

#line 500 "MyFile"
#error MyError // MyError on line 500

Region directive

The last two directives are #region and #endregion. They delimit a section of code that can be expanded or collapsed using the outlining feature of Visual Studio.

#region MyRegion
#endregion

Just as the conditional directives, regions can be nested any number of levels deep.

#region MyRegion
 #region MySubRegion
 #endregion
#endregion