我在 C# 和 Visual Studio '05 中工作...在我的枚举中,如何使用 '?'
字符?我的枚举是
下面:
public enum Questions
{
How_old_are_you?_= 0,//How to set the ? character
What_is_your_name= 1
}
在我添加 '?'
字符后出现了一些错误。
最佳答案
?
不是 C# 标识符(包括枚举值)中的有效字符。我强烈建议您使用类似的东西:
public enum Question
{
[Description("How old are you?")]
Age,
[Description("What is your name?")]
Name
}
或者有 map 或资源文件等 - 只是不要试图将枚举的名称变成自然语言描述。
编辑:作为引用,如果您想从代码中获取Description
,请按照以下方法进行操作(感谢Christian Hayter):
((DescriptionAttribute) Attribute.GetCustomAttribute(
typeof(Question).GetField("Age", BindingFlags.Public | BindingFlags.Static),
typeof(DescriptionAttribute), false)
).Description;
关于c# - 如何使用 ?枚举中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1184064/