C# - #error Preprocessor Directive Example

Learn about the #error 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 use the #error directive then the program will generate a syntax error.

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

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

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

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

using System;

class Program
{
   public static void Main()
   {
        #if (!MACRO2)
            #error MACRO2 is not defined
        #endif
    }
}

Output

Error	1	#error: 'MACRO2 is not defined'	

Explanation

The above program will generate a syntax error because here we used the #error directive with an error message in the Main() method then the specified message will print as a syntax error when we compile the program.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





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