⌘BABAKOV.NET Let's code!
100% [dark] [ru]

Conditional Compilation in C#

I once had a task to compile a program for different devices. A range of devices in my case have specific capabilities and use special libraries, while for some ordinary devices, these libraries need to be excluded. Of course, you can comment out each time, but there are many inclusions and this approach is quite problematic.

But there is a very good way out of this situation, which is called conditional compilation.

Conditional compilation uses directives: #define, #if, #else, #endif.

As usual, I won't explain how to use it, I'll just give an example:

1

#define PARAM1
#if PARAM1
Console.Write("Defined PARAM1");
#else
Console.Write("Not defined PARAM1");
#endif

At this point, the code will be compiled:

Console.Write("Defined PARAM1");


2

//#define PARAM1
#if PARAM1
Console.Write("Defined PARAM1");
#else
Console.Write("Not defined PARAM1");
#endif

At this point, the code will be compiled:

Console.Write("Not defined PARAM1");


That's it in a nutshell.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.