What is #if DEBUG and How to use it in C#?

CsharpServer Side ProgrammingProgramming

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).

The Debug mode does not optimize the binary it produces because the relationship between source code and generated instructions is more complex.

This allows breakpoints to be set accurately and allows a programmer to step through the code one line at a time.

The Debug configuration of your program is compiled with full symbolic debug information which help the debugger figure out where it is in the source code

The release configuration of your program has no symbolic debug information and is fully optimized.

To change the build configuration

From the Build menu, select Configuration Manager, then select Debug or Release.


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


class Program {
   static void Main() {
      #if DEBUG
      Console.WriteLine("You are in debug");

If the Program is running in the debug mode then the If bloc will return true

And prints "You are in debug"

If the program is not in the debug mode then If Debug return false

Published on 05-Aug-2020 12:18:08