#define is a special "before compilation" directive in C # (it is based on the old CPP directive), which defines a preprocessor symbol.
In combination with #if , depending on which characters are defined, different code will be effectively commented out. Because of this, the code in unselected paths should not even be in a compiled state!
In the above example, the DEBUG and MYTEST defined manually at the top (with #define ), so the code βlooksβ like this when it starts the βnormalβ compilation phase: (In C #, this preprocessing is part of the compiler, not a separate tool, which starts first.)
// preprocessor_if.cs //#define DEBUG //#define MYTEST using System; public class MyClass { static void Main() { //#if (DEBUG && !MYTEST) //Console.WriteLine("DEBUG is defined"); //#elif (!DEBUG && MYTEST) //Console.WriteLine("MYTEST is defined"); //#elif (DEBUG && MYTEST) Console.WriteLine("DEBUG and MYTEST are defined"); //#else //Console.WriteLine("DEBUG and MYTEST are not defined"); //#endif } }
The DEBUG symbol is usually set automatically, depending on whether the project is being built for debugging and other symbols can be set as part of the project itself.
Happy coding.
user166390
source share