Why is it impossible to declare a constant like System.Drawing.Color?

Since I use "System.Drawing.Color.Gainsboro" in several places in my application:

if (tb.BackColor.Equals(System.Drawing.Color.Gainsboro)) {

... I wanted to make it permanent. But when I tried:

const System.Drawing.Color PSEUDO_HIGHLIGHT_COLOR = System.Drawing.Color.Gainsboro;

... I realized: "Type" System.Drawing.Color "cannot be declared as const

???

+3
source share
3 answers

, const, , #, . ( factory , , static ).

static readonly .

static readonly Color PSEUDO_HIGHLIGHT_COLOR = Color.Gainsboro;

. 10.4 #

, , sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, .

, null.

+16

, , , Const

MSDN.

+6

System.Drawing.Color is a struct that cannot be declared persistent . Try using instead static readonlyor use the KnownColor enumeration .

+6
source

All Articles