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
?不知道这是一个命令