C# Preprocessor Directives
Preprocessor directives instruct the compiler to preprocess the information before actual compilation begins.
All preprocessor directives start with a #
and are on a single line. Only whitespace characters can appear before a preprocessor directive. Preprocessor directives are not statements, so they do not end with a semicolon (;
).
The C# compiler does not have a separate preprocessor; however, directives are processed as if there were one. In C#, preprocessor directives are used for conditional compilation. Unlike C and C++, they are not used to create macros. A preprocessor directive must be the only instruction on a line.
C# Preprocessor Directives List
The following table lists the preprocessor directives available in C#:
Preprocessor Directive | Description |
---|---|
#define | It is used to define a symbol. |
#undef | It is used to undefine a symbol. |
#if | It is used to test if a symbol is true. |
#else | It is used to create a compound conditional directive, used with #if. |
#elif | It is used to create a compound conditional directive. |
#endif | Specifies the end of a conditional directive. |
#line | It allows you to modify the compiler's line number and (optionally) the file name for errors and warnings. |
#error | It allows generating an error from a specified location in the code. |
#warning | It allows generating a level one warning from a specified location in the code. |
#region | It allows you to specify an expandable or collapsible code block when using the outline feature of the Visual Studio Code Editor. |
#endregion | It marks the end of a #region block. |
#define Preprocessor
The #define preprocessor directive creates a symbolic constant.
define allows you to define a symbol, so that by using the symbol as the expression passed to the #if directive, the expression will return true. Its syntax is as follows:
#define symbol
The following program illustrates this:
Example
#define PI
using System;
namespace PreprocessorDAppl
{
class Program
{
static void Main(string[] args)
{
#if (PI)
Console.WriteLine("PI is defined");
#else
Console.WriteLine("PI is not defined");
#endif
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
PI is defined
Conditional Directives
You can use the #if directive to create a conditional directive. Conditional directives are used to test if a symbol is true. If true, the compiler executes the code between #if and the next directive.
The syntax for conditional directives is:
#if symbol [operator symbol]...
Where symbol is the name of the symbol to test. You can also use true and false, or place a negation operator before the symbol.
Common operators include:
- == (equal to)
- != (not equal to)
- && (and)
- || (or)
You can also group symbols and operators with parentheses. Conditional directives are used to compile code for debugging or specific configurations. A conditional directive starting with the #if directive must explicitly end with a #endif directive.
The following program demonstrates the use of conditional directives:
Example
#define DEBUG
#define VC_V10
using System;
public class TestClass
{
public static void Main()
{
#if (DEBUG && !VC_V10)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && VC_V10)
Console.WriteLine("VC_V10 is defined");
#elif (DEBUG && VC_V10)
Console.WriteLine("DEBUG and VC_V10 are defined");
#else
Console.WriteLine("DEBUG and VC_V10 are not defined");
#endif
Console.ReadKey();
}
}
When the above code is compiled and executed, it produces the following result:
DEBUG and VC_V10 are defined