42

I have this code in my C# class.

#if DEBUG
        private const string BASE_URL = "http://www.a.com/";
#else
        private const string BASE_URL = "http://www.b.com//";
#endif

What I wanted to ask is when does the

#if DEBUG

path in the code get executed?

Does it get executed

  1. When I run up a debug session in Visual Studio?
  2. When I manually run the exe or dll in question from the debug folder?
  3. Any other circumstances I forgot to mention?
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

7 Answers7

60

#if DEBUG It's a preprocessor definition.

It compiles when you define DEBUG constant. And yes, it's default on Debug Build Configuration.

Visual Studio 2010 Project Properties: Visual Studio 2010 Project Properties

If Define DEBUG constant is checked VS will compile:

private const string BASE_URL = "http://www.a.com/";

Else (not checked) VS will compile:

private const string BASE_URL = "http://www.b.com//";
Danpe
  • 18,668
  • 21
  • 96
  • 131
  • Excellent answer. This is still the case in VS2017. – robnick Mar 08 '18 at 01:51
  • In my project, checking or unchecking 'define DEBUG constant' makes no difference. Any if/else/endif statements will still work as if the checkbox is checked. Any idea why? – Jay Jul 18 '21 at 15:02
  • This [answer here](https://stackoverflow.com/a/18441421/6908282) mentions how you can a "custom Configuration" other than "Debug" & "Release" – Gangula Mar 19 '23 at 14:49
14

It's a preprocessor directive. The code in the DEBUG part is compiled when you do a debug build (more specifically when the DEBUG constant is defined). I.e. if you do a debug build BASE_URL will point to www.a.com. Otherwise it will point to www.b.com.

Wug
  • 12,956
  • 4
  • 34
  • 54
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
12

VS 2017 actually sense the active configuration and for example will gray out the debug condition if the release configuration is selected.

enter image description here

Iman
  • 17,932
  • 6
  • 80
  • 90
9

When you compile with the DEBUG directive. So if it's set only the first line will be part of the compiled binary and if it's not set the second line will be part of the binary.

The DEBUG is by default set when you are compiling the debug configuration in VS however you can set it manually for any configuration

Rune FS
  • 21,497
  • 7
  • 62
  • 96
4

That is a "compiler directive", which means it will actually include or exclude the code from the build process (or compiling) based on the #if's that you put in. That being said, the DEBUG symbol is in the properties of your project, and in Visual Studio is generally removed automatically on the "Release" build.

So basically, it doesn't have to be in Visual studio running in debug, and it does not have to be in any certain folder, your code is just built that way.

iMortalitySX
  • 1,478
  • 1
  • 9
  • 23
3

If you are compiling with the DEBUG configuration, the code before the else line will get compiled while the other will not. If you compile in any other configuration, the second line will be compiled while the first will not.

Justin Self
  • 6,137
  • 3
  • 33
  • 48
  • True in the default setup of the projects in VS however not using VS or changing the setup might include the DEBUG symbol in _any_ configuration (including release if you so wish) – Rune FS Oct 18 '12 at 18:02
2

Go to "Project Properties"--> Build Tab of the application. If the Configuration: Active (Debug) then Debug configuration is enabled. Below code will print to console.

#if DEBUG
    Console.WriteLine("in debug mode...");
#endif

If Configuration: Active(Release) then Release configuration is enabled.Below code will print to console.

#if RELEASE
    Console.WriteLine("in release mode...");
#endif

If you want to switch between DEBUG and RELEASE mode use the "Debug/Release/Configuration Manager" drop down right under the Tools Menu.Apologies as most of the developer know it...but is sometimes overlooked and causes confusion why above code is not running correctly.

gyansada
  • 41
  • 2