C# - #if Preprocessor Directive Example

Learn about the #if preprocessor directive example in C# with the help of example.
Submitted by Nidhi, on October 31, 2020 [Last updated : March 23, 2023]

Here, we will check specified macro is defined or not using #if and #else preprocessor directives.

C# program to demonstrate the example of #if preprocessor directive

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

//C# program to demonstrate the
//#if preprocessor directive.

#define MY_MACRO
using System;
using System.Diagnostics;

class Program
   public static void Main()
        #if (MY_MACRO)
            Console.WriteLine("Macro is defined");
            Console.WriteLine("Macro is not defined");


Macro is defined
Press any key to continue . . .


In the above program, we defined a macro "MY_MACRO". Here we created a class Program that contains the Main() method. The Main() method is the entry point for the program. Here, we checked "MY_MACRO" is defined or not. In our program, we already defined the "MY_MACRO" that's why the "Macro is defined" message will be printed on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻

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