Pau*_*els 7 c# compiler-directives
In C, I could declare a compiler directive as follows:
#define MY_NUMBER 10
Run Code Online (Sandbox Code Playgroud)
However, in C#, I only appear to be able to do this:
#define MY_NUMBER
Run Code Online (Sandbox Code Playgroud)
Which is obviously useless in this case.
Is this correct, or am I doing something wrong? If not, can anyone suggest a way of doing this, either at namespace or solution level? I thought of maybe creating a static class, but that seems to be overkill for one value.
小智 5
许多其他答案建议使用public const字段.但请注意,a public const将被编译为引用它的程序集,强制您不仅重新编译它定义的程序集,而且还要重新编译引用它的每个程序集(如果您更改了它的值)const.
如果您不确定该值是否永远不会改变,则public static readonly字段是更好的选择.