Home »
.Net »
C# Programs
C# program to demonstrate the use of #define preprocessor
Here, we are going to learn about the #define preprocessor and its C# implementation.
Submitted by Nidhi, on September 11, 2020
Here we will demonstrate the use of #define preprocessors in the C# program, here we will check defined macros to display messages on the console screen.
Program:
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# program to demonstrate the use of #define preprocessor
#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 »