Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 静态类中的常量实例_C#_Class_Static_Constants - Fatal编程技术网

C# 静态类中的常量实例

C# 静态类中的常量实例,c#,class,static,constants,C#,Class,Static,Constants,我想创建一个全局变量类,下面是我的代码 public static class GLOBALVAR { public static const Color DIFFRENCECOLOR = System.Drawing.Color.LightSalmon; public static const Color NOMATCHCOLOR = System.Drawing.Color.LightBlue; } 但这不想工作,我得到这个错误 The type 'System.

我想创建一个全局变量类,下面是我的代码

public static class GLOBALVAR
{
    public static const Color DIFFRENCECOLOR = System.Drawing.Color.LightSalmon;
    public static const Color NOMATCHCOLOR = System.Drawing.Color.LightBlue;     
}
但这不想工作,我得到这个错误

The type 'System.Drawing.Color' cannot be declared const

有没有办法让这项工作正常进行。

您可以改用
readonly

public static readonly Color DIFFRENCECOLOR = System.Drawing.Color.LightSalmon;
readonly
关键字意味着您的变量
differencecolor
只能在其类的构造函数
GLOBALVAR
中修改

它通常用于在运行时声明“常量”时,但也适用于此目的


您是否尝试过使用
只读
而不是
const
?不知道这是一个命令