Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use #undef directive in C#?
The #undef directive in C# allows you to undefine a previously defined symbol, making it unavailable for use in conditional compilation directives like #if. This is useful for controlling which code sections are included during compilation.
Syntax
Following is the syntax for using the #undef directive −
#undef SYMBOL
Where SYMBOL is the identifier you want to undefine. For example −
#undef DEBUG #undef TESTING
Key Rules
-
The
#undefdirective must appear at the top of the file, before any code or using statements. -
You can only undefine symbols that were previously defined with
#definein the same file. -
An undefined symbol evaluates to
falsein conditional compilation directives. -
Symbols defined via compiler options (like
/define) cannot be undefined with#undef.
Using #undef with Conditional Compilation
The #undef directive works together with #if, #elif, and #else directives to control conditional compilation −
#define DEBUG
#define TESTING
#undef TESTING
using System;
class Program {
static void Main() {
#if DEBUG
Console.WriteLine("Debug mode is enabled");
#endif
#if TESTING
Console.WriteLine("Testing mode is enabled");
#else
Console.WriteLine("Testing mode is disabled");
#endif
#if (DEBUG && TESTING)
Console.WriteLine("Both DEBUG and TESTING are defined");
#elif (DEBUG && !TESTING)
Console.WriteLine("DEBUG is defined, TESTING is undefined");
#else
Console.WriteLine("Neither DEBUG nor TESTING are defined");
#endif
}
}
The output of the above code is −
Debug mode is enabled Testing mode is disabled DEBUG is defined, TESTING is undefined
Practical Example with Feature Flags
Here's a practical example showing how #undef can be used to disable specific features −
#define FEATURE_LOGGING
#define FEATURE_CACHING
#undef FEATURE_CACHING
using System;
class FeatureManager {
static void Main() {
Console.WriteLine("Application started");
#if FEATURE_LOGGING
LogMessage("Feature logging is active");
#endif
#if FEATURE_CACHING
EnableCaching();
#else
Console.WriteLine("Caching is disabled");
#endif
Console.WriteLine("Application ended");
}
#if FEATURE_LOGGING
static void LogMessage(string message) {
Console.WriteLine($"LOG: {message}");
}
#endif
#if FEATURE_CACHING
static void EnableCaching() {
Console.WriteLine("Caching system initialized");
}
#endif
}
The output of the above code is −
Application started LOG: Feature logging is active Caching is disabled Application ended
Common Use Cases
-
Disabling debug features in release builds
-
Temporarily removing features without deleting code
-
Creating different build configurations for testing and production
-
Platform-specific code compilation based on target environment
Conclusion
The #undef directive provides fine-grained control over conditional compilation by allowing you to undefine symbols. It's particularly useful for managing feature flags and creating different build configurations without maintaining separate code files.
