65.9K
CodeProject is changing. Read more.
Home

(C#) Determining whether the current build mode is Debug or Release

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Feb 25, 2012

CPOL
viewsIcon

12660

I prefer the [Conditional("DEBUG")] attribute over #if DEBUG.This is especially advised for your logging example.E.g.[System.Diagnostics.Conditional("DEBUG")]private void Validate() { /* check instance integrity */ }or[System.Diagnostics.Conditional("DEBUG")]public static...

I prefer the [Conditional("DEBUG")] attribute over #if DEBUG. This is especially advised for your logging example. E.g.
[System.Diagnostics.Conditional("DEBUG")]
private void Validate() { /* check instance integrity */ }
or
[System.Diagnostics.Conditional("DEBUG")]
public static void DebugLog(string message) { ... }
You call these methods like any other method. The compiler will not add any call code if "DEBUG" is not defined. BTW: Your resoning of "not Debug" equals "Release" is not true in general, you might have other configurations than only the default "Debug" and "Release" configurations.