c# - 你如何使用#define?

标签 c#

我想知道发送使用#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?

最佳答案

在 C# 中,#define 宏,就像 Bernard 的一些例子,是不允许的。 C# 中 #define/#if 的唯一常见用途是添加可选的仅调试代码。例如:

        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()
        }

关于c# - 你如何使用#define?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15744/

相关文章:

c# - LINQ:在列表中查找频率 = 1 的项目

c# - 如何更改项目的 .sln 文件的位置?

c# - 异步初始化及其单元测试

c# - 为什么在 WCF 中使用 LocalChannel 时执行序列化?

c# - Clean Solution 导致引用错误

c# - 找出方法可能在 C# 中抛出的异常

C# MySql 如果记录存在则更新否则插入?

c# - 如何使用 OAuth 像小部件一样保护客户端脚本?

c# - 开发简单的 C# 应用程序以了解 Resharper Open API

c# - FindResource 无法与位图一起使用