Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method for utilizing customized preprocessor directives in .Net Core is as follows: 1. Define the preprocessor directive using the #define directive followed by the identifier. 2. Use the #if directive to test for the existence of the preprocessor directive. 3. Define the conditional code that should be executed if the preprocessor directive is defined. 4. Use the #else and #endif directives to define the code that should be executed if the preprocessor directive is not defined. 5. To pass the preprocessor directive through to the compiler, use the -define command-line option or the DefineConstants property of the C# compiler in Visual Studio.

For example:

#define DEBUG

#if DEBUG
    // code that should be executed if the DEBUG directive is defined
    Console.WriteLine("Debug mode is on.");
#else
    // code that should be executed if the DEBUG directive is not defined
    Console.WriteLine("Debug mode is off.");
#endif

To pass the DEBUG preprocessor directive through to the compiler, use the following command-line option:

dotnet build -define:DEBUG