C# #if DEBUG 运行时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12960602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:06:48  来源:igfitidea点击:

When #if DEBUG runs

c#.net

提问by Sachin Kainth

I have this code in my C# class.

我的 C# 类中有此代码。

#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?
  1. 当我在 Visual Studio 中运行调试会话时?
  2. 当我从调试文件夹手动运行有问题的 exe 或 dll 时?
  3. 还有什么我忘记提及的情况吗?

采纳答案by Danpe

#if DEBUGIt's a preprocessor definition.

#if DEBUG这是一个预处理器定义。

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

它在您定义 DEBUG 常量时编译。是的,它是调试构建配置的默认设置。

Visual Studio 2010 Project Properties:Visual Studio 2010 Project Properties

Visual Studio 2010 项目属性:Visual Studio 2010 项目属性

If Define DEBUG constantis checked VS will compile:

如果Define DEBUG constant选中 VS 将编译:

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

Else (not checked) VS will compile:

否则(未选中)VS 将编译:

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

回答by Brian Rasmussen

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.

这是一个预处理器指令。DEBUG 部分中的代码在您进行调试构建时编译(更具体地说,在定义 DEBUG 常量时)。即,如果您进行调试构建,BASE_URL 将指向 www.a.com。否则它将指向 www.b.com。

回答by Rune FS

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.

当您使用 DEBUG 指令编译时。因此,如果仅设置了第一行,则将是已编译二进制文件的一部分,如果未设置,则第二行将是二进制文件的一部分。

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

DEBUG 在 VS 中编译调试配置时默认设置,但是您可以为任何配置手动设置它

回答by Justin Self

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.

如果您使用 DEBUG 配置进行编译,else 行之前的代码将被编译,而其他行则不会。如果您在任何其他配置中编译,第二行将被编译,而第一行则不会。

回答by iMortalitySX

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.

这是一个“编译器指令”,这意味着它实际上会根据您输入的 #if 在构建过程(或编译)中包含或排除代码。话虽如此,DEBUG 符号位于您项目的属性中,并且在 Visual Studio 中通常会在“发布”版本中自动删除。

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.

所以基本上,它不必在调试中运行的 Visual Studio 中,也不必在任何特定文件夹中,您的代码就是这样构建的。

回答by Iman

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

VS 2017 实际上会感知活动配置,例如,如果选择了发布配置,则调试条件会变灰

enter image description here

在此处输入图片说明

回答by gyansada

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.

如果 Configuration: Active(Release) 则 Release configuration 已启用。下面的代码将打印到控制台。

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

如果您想在 DEBUG 和 RELEASE 模式之间切换,请使用“工具菜单”下方的“调试/发布/配置管理器”下拉菜单。抱歉,大多数开发人员都知道这一点......但有时会被忽视并导致混淆为什么上面的代码是没有正确运行。