C# 你如何使用#define?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15744/
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
How do you use #define?
提问by MaseBase
I'm wondering about instances when it makes sent to use #define and #if statements. I've known about it for a while, but never incorporated it into my way of coding. How exactly does this affect the compilation?
我想知道它何时发送使用 #define 和 #if 语句的实例。我知道它有一段时间了,但从未将其纳入我的编码方式。这究竟如何影响编译?
Is #define the only thing that determines if the code is included when compiled? If I have #define DEBUGme as a custom symbol, the only way to exclude it from compile is to remove this #define statement?
#define 是确定编译时是否包含代码的唯一因素吗?如果我将#define DEBUGme 作为自定义符号,从编译中排除它的唯一方法是删除此#define 语句?
采纳答案by Eric Haskins
In C# #define
macros, like some of Bernard's examples, are not allowed. The only common use of #define
/#if
s in C# is for adding optional debug only code. For example:
在 C# 中#define
,不允许使用宏,例如 Bernard 的一些示例。C#中#define
/ #if
s的唯一常见用途是添加可选的仅调试代码。例如:
static void Main(string[] args)
{
#if DEBUG
//this only compiles if in DEBUG
Console.WriteLine("DEBUG")
#endif
#if !DEBUG
//this only compiles if not in DEBUG
Console.WriteLine("RELEASE")
#endif
//This always compiles
Console.ReadLine()
}
回答by Ed S.
Well, defines are used often for compile time constants and macros. This can make your code a bit faster as there are really no function calls, the output values of the macros are determined at compile time. The #if's are very useful. The most simple example that I can think of is checking for a debug build to add in some extra logging or messaging, maybe even some debugging functions. You can also check different environment variables this way.
好吧,定义经常用于编译时常量和宏。这可以使您的代码更快一点,因为实际上没有函数调用,宏的输出值是在编译时确定的。#if 非常有用。我能想到的最简单的例子是检查调试版本以添加一些额外的日志记录或消息传递,甚至可能是一些调试功能。您还可以通过这种方式检查不同的环境变量。
Others with more C/C++ experience can add more I am sure.
我相信其他具有更多 C/C++ 经验的人可以添加更多。
回答by HS.
I often find myself defining some things that are done repetitively in certain functions. That makes the code much shorter and thus allows a better overview.
我经常发现自己定义了一些在某些功能中重复完成的事情。这使得代码更短,从而允许更好的概览。
But as always, try to find a good measure to not create a new language out of it. Might be a little hard to read for the occasional maintenance later on.
但与往常一样,尝试找到一个很好的衡量标准,不要从中创建一种新语言。以后偶尔的维护可能会有点难以阅读。
回答by Mark Cidade
#define is used to define compile-time constants that you can use with #if to include or exclude bits of code.
#define 用于定义编译时常量,您可以将这些常量与 #if 一起使用以包含或排除代码位。
#define USEFOREACH
#if USEFOREACH
foreach(var item in items)
{
#else
for(int i=0; i < items.Length; ++i)
{ var item = items[i]; //take item
#endif
doSomethingWithItem(item);
}
回答by OJ.
@Ed: When using C++, there is rarely any benefit for using #define over inline functions when creating macros. The idea of "greater speed" is a misconception. With inline functions you get the same speed, but you also get type safey, and no side-effects of preprocessor "pasting" due to the fact that parameters are evaluated before the function is called (for an example, try writing the ubiquitous MAX macro, and call it like this: MAX(x++, y).. you'll see what I'm getting at).
@Ed:在使用 C++ 时,在创建宏时使用 #define 而不是内联函数几乎没有任何好处。“更快的速度”的想法是一种误解。使用内联函数,您可以获得相同的速度,但您也可以获得类型安全,并且没有预处理器“粘贴”的副作用,因为在调用函数之前评估参数(例如,尝试编写无处不在的 MAX 宏,并这样称呼它:MAX(x++, y)..你会明白我在说什么)。
I have never had to use #define in my C#, and I very rarely use it for anything other that platform and compiler version checking for conditional compilation in C++.
我从来没有在我的 C# 中使用 #define,而且我很少将它用于任何其他平台和编译器版本检查,以便在 C++ 中进行条件编译。
回答by roo
Is #define the only thing that determines if the code is included when compiled? If I have #define DEBUGme as a custom symbol, the only way to exclude it from compile is to remove this #define statement?
#define 是确定编译时是否包含代码的唯一因素吗?如果我将#define DEBUGme 作为自定义符号,从编译中排除它的唯一方法是删除此#define 语句?
You can undefinesymbols as well
您也可以取消定义符号
#if defined(DEBUG)
#undef DEBUG
#endif
回答by ICR
Perhaps the most common usees of #define in C# is to differentiate between debug/release and different platforms (for example Windows and X-Box 360 in the XNA framework).
也许#define 在 C# 中最常见的用途是区分调试/发布和不同平台(例如 XNA 框架中的 Windows 和 X-Box 360)。
回答by Ali Parr
It's for conditional compilation, so you can include or remove bits of code based upon project attributes which tend to be:
它用于条件编译,因此您可以包含或删除基于项目属性的代码位,这些属性往往是:
- Intended platform (Windows/Linux/XB360/PS3/Iphone.... etc)
- Release or Debug (Generally logging, asserts etc are only included in a debug build)
- 目标平台(Windows/Linux/XB360/PS3/Iphone....等)
- 发布或调试(通常日志记录、断言等仅包含在调试版本中)
They can also be used to disable large parts of a system quickly, for example, during development of a game, I might define
它们还可用于快速禁用系统的大部分,例如,在游戏开发过程中,我可能会定义
#define PLAYSOUNDS
and then wrap the final call to play a sound in:
然后包装最终调用以在以下位置播放声音:
#ifdef PLAYSOUNDS
// Do lots of funk to play a sound
return true;
#else
return true;
So it's very easy for me to turn on and off the playing of sounds for a build. (Typically I don't play sounds when debugging because it gets in the way of my personal music :) ) The benefit is that you're not introducing a branch through adding an if statement....
所以我很容易打开和关闭构建的声音播放。(通常我在调试时不播放声音,因为它妨碍了我的个人音乐:))好处是你不会通过添加 if 语句来引入分支......