JYelton
JYelton

Reputation: 36512

Defining colors as constants in C#

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

Upvotes: 18

Views: 22316

Answers (6)

sx2009
sx2009

Reputation: 15

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);

Upvotes: -1

Greg Beech
Greg Beech

Reputation: 136627

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.

Upvotes: 2

codymanix
codymanix

Reputation: 29490

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.

Upvotes: 24

Jon
Jon

Reputation: 437376

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.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160902

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.

Upvotes: 6

John Leidegren
John Leidegren

Reputation: 60997

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

Upvotes: 1

Related Questions