gpt4 book ai didi

c# - #在 C# 中定义编译器指令

转载 作者:太空狗 更新时间:2023-10-29 19:55:21 25 4
gpt4 key购买 nike

在 C 中,我可以如下声明一个编译器指令:

#define MY_NUMBER 10

但是,在 C# 中,我似乎只能这样做:

#define MY_NUMBER

在这种情况下,这显然是无用的。

这是正确的,还是我做错了什么?如果没有,任何人都可以建议在 namespace 或解决方案级别执行此操作的方法吗?我想过也许创建一个静态类,但这对于一个值来说似乎有点过分了。

最佳答案

是的,这是正确的。

这里引用自 MSDN 文档:

The pre-processing directives provide the ability to conditionally skip sections of source files, to report error and warning conditions, and to delineate distinct regions of source code. The term "pre-processing directives" is used only for consistency with the C and C++ programming languages. In C#, there is no separate pre-processing step; pre-processing directives are processed as part of the lexical analysis phase.

所以你不能像在 C 和 C++ 中那样真正定义编译器常量。

相关资源:

关于c# - #在 C# 中定义编译器指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3012686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com