在 C# 中将颜色定义为常量
我在 C# winforms 应用程序中设置了一些默认颜色,如下所示:
readonly Color ERROR = Color.Red;
readonly Color WARNING = Color.Orange;
readonly Color OK = Color.Green;
据我所知,readonly 对于我的目的来说本质上是一个常量。如果我尝试将它们定义为常量,编译器会指示它必须是编译时常量,而 Color 不是。
我可以保持原样吗?或者有什么方法可以定义我应该知道的这些常量吗?
(目的只是为了有一个位置可以更改所有颜色以进行记录。)
I've set up some default colors in a C# winforms application like so:
readonly Color ERROR = Color.Red;
readonly Color WARNING = Color.Orange;
readonly Color OK = Color.Green;
As far as I am aware, readonly is essentially a constant for my purposes. If I attempt to define these as constants, the compiler indicates that it must be a compile-time constant, which Color is not.
Am I good leaving these as-is, or is there some way to define these constants that I should be aware of?
(The purpose is simply to have a single location in which to change all of the colors for logging purposes.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只有文字才能定义为 const。不同之处在于,
const
值被硬烘焙到使用它的程序集中。如果它们的定义发生变化,那么调用站点不会注意到,除非它们被重新编译。相比之下,
readonly
声明变量的方式不能在构造函数(或在static readonly
的情况下为静态构造函数)之外重新赋值。多变的)。因此,您没有其他方法可以在此处使用 readonly,因为 Color 是一个结构体,并且没有原始数据类型或文字。
Only literals can be defined as
const
. The difference is, thatconst
values are hard-bakened into the assemblies that uses it. Should their definition change, then the call sites doesn't notice unless they get recompiled.In contrast,
readonly
declares a variable in a way that it cannot be reassigned after outside the constructor (or static constructor in case of astatic readonly
variable).So, you have no other way then to use readonly here, since Color is a struct, and no primitive data type or literal.
const 字段是一个编译时常量 - 您实际上需要运行代码来确定 Color.Orange 的值,但内部可能定义为
由于这无法在编译时计算,因此您唯一的选择是在运行时设置的
readonly
。另请查看 这篇文章。
A
const
field is acompile time
constant - you actually need code to run to determine the value ofColor.Orange
though, internally probably defined asSince this cannot be computed at compile time, your only option is
readonly
which is set at runtime.Also check out this article.
除了其他人提到的技术方面(
const
值在编译时在使用它们的地方被替换,并且需要是文字而不是static readonly
)在运行时分配和引用的值)需要考虑语义问题。const
值在编译时被替换的原因是const
确实意味着“常量” - 就像永远不会改变的值,例如 pi 或e。这就是为什么在编译时替换它们是安全的,因为名称代表了永远不变的值。你所说的事实...
...表明这些在语义上不是常量,因此不应定义为 const,即使可以这样做。
Aside from the technical aspects that others have mentioned (that
const
values are replaced at compile-time in the places they are used, and are required to be literals rather thanstatic readonly
values which are assigned and referenced at runtime) there is a semantic issue to consider.The reason
const
values are replaced at compile-time is thatconst
really does mean "constant" - as in a value that will never, ever change such as pi or e. That's why it's safe to replace them at compile-time, because the name represents a forever unchanging value.The fact that you state...
...indicates that these are not semantically constant, and thus should not be defined as
const
even if it were possible to do so.你至少可以让它们静态。只读字段就是一个只能在初始化期间分配的字段。它不保证所表示的值是“只读”的。
You could at the very least make 'em static. A read-only field is otherwise just that, a field, that can only be assigned to during initilization. It makes no guarantees about the represented value being "read-only".
这很好,而且你不能做得更好(正如编译器告诉你的那样)。
但如果还没有的话,请务必将它们设置为
静态
。This is fine, and you can't do any better (as the compiler tells you).
But do make them
static
, if they are not already.您可以像这样定义静态颜色:
You can define a static Colors like this: