What is if/then directives for debug vs release in C#?


In Visual Studio Debug mode and Release mode are different configurations for building your .Net project.

Select the Debug mode for debugging step by step their .Net project and select the Release mode for the final build of Assembly file (.dll or .exe).

To change the build configuration

From the Build menu, select Configuration Manager, then select Debug or Release. Or On the toolbar, choose either Debug or Release from the Solution Configurations list.

The code which is written inside the #if debug will be executed only if the code is running inside the debug mode.

If the code is running in the release mode, then the #if Debug will be false and it will not execute the code present inside this.

Example

class Program{
   static void Main(string[] args){
      #if DEBUG
         Console.WriteLine("Mode=Debug");
      #else
         Console.WriteLine("Mode=Release");
      #endif
         Console.ReadLine();
   }
}

Output

if in Debug Mode
Mode=Debug
if in Release Mode
Mode=Release

Updated on: 19-Aug-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements