c# - 如何使用 ?枚举中的字符

标签 c#

我在 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/

相关文章:

c# - Asp.net 开源项目作为 c# 中的学习资源

c# - 如何优化此代码以创建 TIFF 文件和/或我应该考虑使用哪些替代库来提高性能?

c# - '一个:Action' must be understood but cannot be handled and RequestSecurityToken problem on an ONVIF Camera

c# - .OrderBy 函数的 System.Linq.Expressions.Expression

C# lambda 表达式反向 <=

c# - RSS Feeds 1 秒或更少更新

c# - 使用Session验证用户名和密码

C# 转换泛型类型(如 C++ 用户定义的转换)

c# - 连接未关闭连接的当前状态是打开的

c# - c# - web api中 "$"关键字的用途是什么