Is it possible to use preprocessor definitions in C# like we do in C and C++?
Code:#define DEBUG
// ...
#ifdef DEBUG
Console.WriteLine("This is a debug message.");
#endif // #ifdef DEBUG
Printable View
Is it possible to use preprocessor definitions in C# like we do in C and C++?
Code:#define DEBUG
// ...
#ifdef DEBUG
Console.WriteLine("This is a debug message.");
#endif // #ifdef DEBUG
Yes, you can define them in Properties > Build. The "DEBUG" symbol is already defined for you.
For your sample, the only thing that changes is "#ifdef DEBUG" becomes "#if DEBUG".
Note that C# #define's are strictly 'flag-type' #defines - you can't specify replacements or macros as you can in C/C++.
For conditional debugging it is possible, but nothing like C++ macros are available in C#.
Symbol checked in #if condition can be also defined when invoking compiler from command line with /define: e.g. csc /define: DEBUG.