在 C# 中将颜色定义为常量

发布于 2024-10-24 08:26:20 字数 342 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

妄断弥空 2024-10-31 08:26:20

只有文字才能定义为 const。不同之处在于,const 值被硬烘焙到使用它的程序集中。如果它们的定义发生变化,那么调用站点不会注意到,除非它们被重新编译。

相比之下,readonly 声明变量的方式不能在构造函数(或在 static readonly 的情况下为静态构造函数)之外重新赋值。多变的)。

因此,您没有其他方法可以在此处使用 readonly,因为 Color 是一个结构体,并且没有原始数据类型或文字。

Only literals can be defined as const. The difference is, that const 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 a static 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.

丶情人眼里出诗心の 2024-10-31 08:26:20

const 字段是一个编译时常量 - 您实际上需要运行代码来确定 Color.Orange 的值,但内部可能定义为

public static readonly Color Orange = new Color(...);

由于这无法在编译时计算,因此您唯一的选择是在运行时设置的 readonly

另请查看 这篇文章

A const field is a compile time constant - you actually need code to run to determine the value of Color.Orange though, internally probably defined as

public static readonly Color Orange = new Color(...);

Since this cannot be computed at compile time, your only option is readonly which is set at runtime.

Also check out this article.

感性 2024-10-31 08:26:20

除了其他人提到的技术方面(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 than static 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 that const 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...

The purpose is simply to have a single location in which to change all of the colors for logging purposes.

...indicates that these are not semantically constant, and thus should not be defined as const even if it were possible to do so.

走走停停 2024-10-31 08:26:20

你至少可以让它们静态。只读字段就是一个只能在初始化期间分配的字段。它不保证所表示的值是“只读”的。

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".

原来分手还会想你 2024-10-31 08:26:20

这很好,而且你不能做得更好(正如编译器告诉你的那样)。

但如果还没有的话,请务必将它们设置为静态

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.

嗫嚅 2024-10-31 08:26:20

您可以像这样定义静态颜色:

// tested with C# 5.0
static const Color ERROR = Color.FromArgb(0, 255,0);
static const Color MYPOOL = Color.FromKnownColor(KnownColor.Aqua);

You can define a static Colors like this:

// tested with C# 5.0
static const Color ERROR = Color.FromArgb(0, 255,0);
static const Color MYPOOL = Color.FromKnownColor(KnownColor.Aqua);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文