C# program to demonstrate the #if preprocessor directive

Here, we are going to demonstrate the #if preprocessor directive in C#?
Submitted by Nidhi, on October 31, 2020

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

Program:

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");
        #else
            Console.WriteLine("Macro is not defined");
        #endif
    }
}

Output:

Macro is defined
Press any key to continue . . .

Explanation:

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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.