When #if DEBUG runs

后端 未结 7 592
星月不相逢
星月不相逢 2020-12-14 06:03

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 = \"h         


        
相关标签:
7条回答
  • 2020-12-14 06:46

    #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//";
    
    0 讨论(0)
  • 2020-12-14 06:48

    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.

    0 讨论(0)
  • 2020-12-14 06:51

    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.

    0 讨论(0)
  • 2020-12-14 06:52

    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.

    0 讨论(0)
  • 2020-12-14 06:53

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

    0 讨论(0)
  • 2020-12-14 07:05

    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.

    0 讨论(0)
提交回复
热议问题