Problem
For my one and only project, I have Configuration set to “release” under Solution properties.
I have this code at the start of the main procedure, and it says “Mode=Debug.” These two lines are also at the very top:
#define DEBUG
#define RELEASE
Is it possible that I’m testing the wrong variable?
#if (DEBUG)
Console.WriteLine("Mode=Debug");
#elif (RELEASE)
Console.WriteLine("Mode=Release");
#endif
My goal is to establish various defaults for variables depending on whether the mode is debug or release.
Asked by NealWalters
Solution #1
VS should already have DEBUG/ DEBUG defined.
In your code, remove the #define DEBUG. Set preprocessors for that specific build in the build configuration.
Because of your #define, it prints “Mode=Debug” before skipping the elif.
The correct technique to check is as follows:
#if DEBUG
Console.WriteLine("Mode=Debug");
#else
Console.WriteLine("Mode=Release");
#endif
Don’t look for the word RELEASE.
Answered by psychotik
Solution #2
When a project is compiled in Debug mode, Visual Studio defines DEBUG by default, but not when it is compiled in Release mode. By default, RELEASE is not defined in Release mode. Use a phrase like this:
#if DEBUG
// debug stuff goes here
#else
// release stuff goes here
#endif
If you simply want to accomplish something in release mode, follow these steps:
#if !DEBUG
// release...
#endif
It’s also worth noting that on methods that return void, you may use the [Conditional(“DEBUG”)] property to ensure that they are only executed if a specific symbol is declared. If the symbol isn’t declared, the compiler will eliminate any calls to those methods:
[Conditional("DEBUG")]
void PrintLog() {
Console.WriteLine("Debug info");
}
void Test() {
PrintLog();
}
Answered by mmx
Solution #3
Rather than looking for #define directives, I prefer to check it like this:
if (System.Diagnostics.Debugger.IsAttached)
{
//...
}
else
{
//...
}
With the caveat that you could compile and deploy something in debug mode even if the debugger isn’t attached.
Answered by Joel Coehoorn
Solution #4
I’m not a fan of the #if stuff, especially if you use it across your code base, because it may cause issues where Debug builds pass but Release builds fail if you’re not cautious.
So here is what I came up with (influenced by C#ifdef): #’s
public interface IDebuggingService
{
bool RunningInDebugMode();
}
public class DebuggingService : IDebuggingService
{
private bool debugging;
public bool RunningInDebugMode()
{
//#if DEBUG
//return true;
//#else
//return false;
//#endif
WellAreWe();
return debugging;
}
[Conditional("DEBUG")]
private void WellAreWe()
{
debugging = true;
}
}
Answered by Tod Thomson
Solution #5
bool isDebug = false;
Debug.Assert(isDebug = true); // '=', not '=='
Debug is a method. DEBUG is a conditional attribute on Assert. The call and the assignment isDebug = true are removed if it is not defined:
isDebug is set to true if DEBUG is defined (and passed to Debug.Assert , which does nothing in that case).
Answered by AlexD
Post is based on https://stackoverflow.com/questions/2104099/c-sharp-if-then-directives-for-debug-vs-release