C# - #define Preprocessor Example

In this example, we are going to learn how to implement #define preprocessor using C# program?
Submitted by Nidhi, on September 11, 2020 [Last updated : March 22, 2023]

Here we will demonstrate the use of #define preprocessor in the C# program, here we will check defined macros to display messages on the console screen.

C# program to demonstrate the example of #define preprocessor

The source code to demonstrate the use of #define preprocessor is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - #define Preprocessor Example

#define PRINT_MSG_TYPE1

using System;
class Program
{
    static void Main()
    {
        #if PRINT_MSG_TYPE1
            Console.WriteLine("Print message type1 on console screen");
        #endif

        #if PRINT_MSG_TYPE2
            Console.WriteLine("Print message type2 on console screen");
        #endif
    }
}

Output

Print message type1 on console screen
Press any key to continue . . .

Explanation

In the above program, we demonstrate the use of #define macro. Here we defined a macro PRINT_MSG_TYPE1 at the top in our program. Then we checked defined macros in the Main() method. Here we defined PRINT_MSG_TYPE1 macro but we did not define PRINT_MSG_TYPE2 macro that's why the message "Print message type1 on console screen" is printed.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.