More about #define, #undef and Conditional Compilation Directives in C# (C Sharp)

This article will guide you in understanding six different preprocessor directives namely #def, #undef, #if, #elif, #else, #endif. The last four directives are termed as conditional compilation directives.

#define, #undef Directives:

#define and #undef directives are mainly used for testing. If you are running your program in debug mode, you might want certain block of code to be executed to test certain testcases. However execution of that portion of code is not required when the code moves to production.

How will you do it? Normally you will include that particular block of code in your application at the time of testing and remove those code portions at the time of actual implementation. What if you forget to remove it? Or after removing, what if you want the code again for further debugging? To make such testing simpler, you can use #define directive. Here’s an example:

#define DEBUGMODE
#if DEBUGMODE
Console.WriteLine(“In Debug Mode”);
//test specific cases
#endif

The identifier used along with #define will always return the Boolean value “true”. Hence the output will be “In Debug Mode”. What if the code goes to production? Since #define DEBUGMODE is used, compiler runs the code in if-block. All that you have to do is change the #define directive to #undef directive. Here is the modified version of your code:

#undef DEBUGMODE
#if DEBUGMODE
Console.WriteLine(“In Debug Mode”);
//test specific cases
#endif

When an identifier is associated with #undef, it will always return false. Hence if-condition will fail and the debug mode specific code will not be executed. This solution expects you to change only a single line of code during your testing.

However, if you forget to change this line of code, then again you will face issues. There is one more alternative solution which doesn’t expect you to change even a single line of code. You have to mention the directives at command line during compilation time. Your code will now be:

#define DEBUGMODE
public class testClass {
public static void Main() {
#if DEBUGMODE
Console.WriteLine(“In Debug Mode”);
//test specific cases
#endif
}
}

When you want the code to be executed in debug mode, type the following line in command prompt:

csc /define:DEBUGMODE testClass.aspx

When your code is in implementation mode, type the following line in command prompt:
csc testClass.aspx

Since you do not specify /define, DEBUGMODE will be set to false and therefore if-block will not be executed. There is one more alternative to it. You can type /undef to clearly specify that the identifier has to be set to false.

csc /undef:DEBUGMODE testClass.aspx

If your code requires multiple #define directives, then you can specify all of them in command prompt by separating the identifiers using the delimiter semicolon.

There is a constraint to be noted while using #define directive. You have to use this directive before any C# code. If you are using it in between C# code, it will throw an error. Here is an example for the incorrect approach:

public class testClass {
public static void Main() {
#define DEBUGMODE
#if DEBUGMODE
Console.WriteLine(“In Debug Mode”);
//test specific cases
#endif
}
}

One more interesting feature about #define directive is that this directive can be redefined for the same identifier. This is perfectly legal. Here’s an example:

#define DEBUGMODE // DEBUGMODE = true
#undef DEBUGMODE // DEBUGMODE = false
#define DEBUGMODE // DEBUGMODE = true again

Directives Performing Conditional Compilation:

If you have to execute or skip certain block of code based on certain conditions, you can do it by using four different directives namely #if, #elif, #else, #endif. Here is an example containing these directives:

#define DEBUGMODE
#undef PRODUCTIONMODE
#if DEBUGMODE && !PRODUCTIONMODE
//perform specific test cases related to debug mode only
#elif PRODUCTIONMODE && !DEBUGMODE
//perform specific test cases related to debug mode only
#else
//execute this block of code when none of the earlier elif conditions match
#endif

From the example above, it is obvious that if #if directive is present then it should have a corresponding #endif directive. #elif directive does the job of “else if” statements. Control will come to #elif only if #if directives associated condition fails. In this example you have two identifiers, one for DEBUGMODE and the other for PRODUCTIONMODE.

At a point of time, either DEBUGMODE or PRODUCTIONMODE will be active but not both. #if directive determines what block of code has to be executed when DEBUGMODE alone is active. #elif performs execution of its block of code if PRODUCTIONMODE alone is active. Instead of having a separate #if #endif block, you can club this condition check using #elif since the check is done on the same identifiers. All other possible combinations of DEBUGMODE and PRODUCTIONMODE fall under else block. Each #if block has to be finally terminated with an #endif block.

|How and Why to Use Trace Option for Debugging in ASP.NET Application | How to Display Tool Tip in Your ASP.NET Web Application | How to Use “const” Class Member Modifier in C# | How to Use HTML Help Viewer in Your ASP.NET Web Application | List of Preprocessor Directives Available in C# | More about #define, #undef and Conditional Compilation Directives in C# |Understanding Different Class Member Modifiers in C# | Understanding of Diagnostic Directives and #pragma, #line, #region, #endregion Directives in C# | Usage of Roles API to Perform Authorization in .NET| What are the Different Compiler Engineering Techniques|Writing Unsafe Code in C#|

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.