This question already has answers here:
How to include an enum value in a const string?
(3个答案)
已关闭6年。
我有一个很大的C#类,只剩下
但是,出现此编译器错误:
我知道我可以删除
(3个答案)
已关闭6年。
我有一个很大的C#类,只剩下
public const string
字段。但是,在一种情况下,我试图将字符串和枚举值连接到const string
字段中,如下所示:public const string GET_VALUES = "SELECT * FROM [tbl] WHERE [id] = " + Enum.Val;
但是,出现此编译器错误:
'Namespace.SqlStatements.GET_VALUES' must be constant
我知道我可以删除
const
子句,但我想使此类中的所有字段保持一致。是否可以在C#中连接常量字符串和枚举? 最佳答案
从MSDN:
在您的情况下,必须使用ToString
将枚举转换为字符串,这在编译时是不可能的。我建议您像elgonzo所提到的那样将其更改为readonly
。
关于c# - 连接常量字符串和枚举,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24953789/