
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. Heres
an example: #define DEBUGMODE 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 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 doesnt 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 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: 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 { One more
interesting feature about #define directive is that this directive can
be redefined for the same identifier. This is perfectly legal. Heres
an example: #define DEBUGMODE
// DEBUGMODE = true 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 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.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |