C# - #undef Preprocessor Directive Example

Learn about the #undef 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 undefine the defined macro using the #undef pre-processor directive.

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

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

//C# - #undef Preprocessor Directive Example.

#define MACRO1
#define MACRO2
#define MACRO3
#undef  MACRO2

using System;

class Program
{
   public static void Main()
    {
        #if (MACRO1)
            Console.WriteLine("MACRO1 is defined");
        #endif
        #if (MACRO2)
            Console.WriteLine("MACRO2 is defined");
        #endif
        #if (MACRO3)
            Console.WriteLine("MACRO3 is defined");
        #endif
    }
}

Output

MACRO1 is defined
MACRO3 is defined
Press any key to continue . . .

Explanation

In the above program, we defined three macros MACRO1, MACRO2, and MACRO3. After that, we undefined the MACRO2 using the #undef pre-processor directive.

Here, we created a class Program that contains the Main() method. The Main() method is the entry point for the program. Here we checked macros are defined or not and print the appropriate message on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





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