How to use #error and #warning directives in C#?

The #error and #warning directives in C# are preprocessor directives that allow developers to generate custom compiler errors and warnings during the compilation process. These directives are useful for conditional compilation scenarios and providing feedback about missing configurations or deprecated code paths.

Syntax

Following is the syntax for the #error directive −

#error error-message

Following is the syntax for the #warning directive −

#warning warning-message

Using #error Directive

The #error directive generates a compile-time error with a custom message. This prevents the code from compiling and forces the developer to address the issue before proceeding.

#error Directive Flow Condition Check Met #error Triggered Compilation Fails Prevents successful compilation until condition is resolved

Example

using System;

namespace Demo {
    class Program {
        public static void Main(string[] args) {
            #if (!ONE)
            #error ONE is undefined
            #endif
            Console.WriteLine("This line will not execute due to compilation error!");
        }
    }
}

The output of the above code is −

Compilation failed: 1 error(s), 0 warnings
error CS1029: #error: 'ONE is undefined'

Using #warning Directive

The #warning directive generates a compile-time warning but allows compilation to continue. This is useful for notifying developers about potential issues or deprecated code paths.

Example

using System;

namespace Demo {
    class Program {
        public static void Main(string[] args) {
            #if (!TWO)
            #warning TWO is undefined - using default behavior
            #endif
            Console.WriteLine("Generates a warning but continues execution!");
        }
    }
}

The output of the above code is −

warning CS1030: #warning: 'TWO is undefined - using default behavior'
Generates a warning but continues execution!

Practical Use Cases

These directives are commonly used for configuration validation and development workflow management −

Example

using System;

namespace ConfigValidation {
    class Program {
        public static void Main(string[] args) {
            #if DEBUG
            #warning Debug mode is active - performance may be reduced
            #endif
            
            #if (!PRODUCTION && !DEBUG)
            #error Neither PRODUCTION nor DEBUG is defined - please specify build configuration
            #endif
            
            Console.WriteLine("Application running successfully!");
            
            #if DEBUG
            Console.WriteLine("Debug information: Application started at " + DateTime.Now);
            #endif
        }
    }
}

The output of the above code is −

warning CS1030: #warning: 'Debug mode is active - performance may be reduced'
Application running successfully!
Debug information: Application started at 12/15/2024 10:30:45 AM

Comparison

Directive Compilation Result Use Case
#error Compilation fails completely Critical configuration issues, missing dependencies
#warning Compilation succeeds with warning Non-critical issues, deprecated features, debug notifications

Conclusion

The #error and #warning directives provide powerful tools for controlling the compilation process in C#. Use #error to prevent compilation when critical conditions are not met, and #warning to alert developers about potential issues while allowing the build to continue.

Updated on: 2026-03-17T07:04:35+05:30

615 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements