I work in C#, Visual Studio '05... In my enum, how can I use the '?'
character? My enum is below:
我在C#,Visual Studio '05工作......在我的枚举中,我怎么能用'?'字符?我的枚举如下:
public enum Questions
{
How_old_are_you?_= 0,//How to set the ? character
What_is_your_name= 1
}
After I add the '?'
character there are some errors.
我添加'?'后字符有一些错误。
6 个解决方案
#1
?
isn't a valid character in a C# identifier (including enum values). I strongly suggest you use something like:
?不是C#标识符中的有效字符(包括枚举值)。我强烈建议你使用类似的东西:
public enum Question
{
[Description("How old are you?")]
Age,
[Description("What is your name?")]
Name
}
Or have a map or a resource file etc - just don't try to make the name of the enum into a natural language description.
或者有地图或资源文件等 - 只是不要试图将枚举的名称变成自然语言描述。
EDIT: For reference, if you want to get at the Description
from code, here's how to do it (thanks Christian Hayter):
编辑:作为参考,如果你想从代码中获取描述,这里是如何做到的(感谢Christian Hayter):
((DescriptionAttribute) Attribute.GetCustomAttribute(
typeof(Question).GetField("Age", BindingFlags.Public | BindingFlags.Static),
typeof(DescriptionAttribute), false)
).Description;
#2
You can't. ? is an operator, so a sequence of characters containing it is not a valid identifier.
你不能。 ?是一个运算符,因此包含它的字符序列不是有效的标识符。
#3
You can not use question marks in the enum identifiers. The identifiers in an enumeration follows the same rules as other identifiers.
您不能在枚举标识符中使用问号。枚举中的标识符遵循与其他标识符相同的规则。
You can use the @ character to use reserved keywords as identifiers, but you still can't use characters that are not allowed in an identifier.
您可以使用@字符将保留关键字用作标识符,但仍然不能使用标识符中不允许的字符。
#4
You can add q after the question to make it descriptive. Ex:
您可以在问题后添加q以使其具有描述性。例如:
How_old_are_you_q= 0, //Replace ? with q
What_is_your_name_q= 1
#5
Another possible solution could be a mix between an enum and a dictionary :
另一种可能的解决方案可能是枚举和字典之间的混合:
public enum Question
{
Age,
Name
}
...
Dictionary<Question,string> Questions = new Dictionary<Question,string>();
Questions.Add(Question.Age, "How old are you ?");
Questions.Add(Question.Name, "What is your name ?");
#6
You can tag your Enum
items with underscore characters, which are allowed in Enum
, and replace them for display.
您可以使用下划线字符标记Enum项目,这些字符在Enum中是允许的,并替换它们以供显示。
Eg Binding an Enum
to a ComboBox
, say, replacing first "__" with "?", then "_" with space Enum
item How_old_are_you__
becomes ComboBox
item "How old are you?"
例如,将枚举绑定到ComboBox,例如,将第一个“__”替换为“?”,然后将“_”替换为空格枚举项目How_old_are_you__变为ComboBox项目“你多大了?”
I use this when I want to stick with Enum
over other options. It works well for partially loading Enum
items into lists and display controls as well.
当我想坚持使用Enum而不是其他选项时,我会使用它。它适用于将Enum项目部分加载到列表和显示控件中。
#1
?
isn't a valid character in a C# identifier (including enum values). I strongly suggest you use something like:
?不是C#标识符中的有效字符(包括枚举值)。我强烈建议你使用类似的东西:
public enum Question
{
[Description("How old are you?")]
Age,
[Description("What is your name?")]
Name
}
Or have a map or a resource file etc - just don't try to make the name of the enum into a natural language description.
或者有地图或资源文件等 - 只是不要试图将枚举的名称变成自然语言描述。
EDIT: For reference, if you want to get at the Description
from code, here's how to do it (thanks Christian Hayter):
编辑:作为参考,如果你想从代码中获取描述,这里是如何做到的(感谢Christian Hayter):
((DescriptionAttribute) Attribute.GetCustomAttribute(
typeof(Question).GetField("Age", BindingFlags.Public | BindingFlags.Static),
typeof(DescriptionAttribute), false)
).Description;
#2
You can't. ? is an operator, so a sequence of characters containing it is not a valid identifier.
你不能。 ?是一个运算符,因此包含它的字符序列不是有效的标识符。
#3
You can not use question marks in the enum identifiers. The identifiers in an enumeration follows the same rules as other identifiers.
您不能在枚举标识符中使用问号。枚举中的标识符遵循与其他标识符相同的规则。
You can use the @ character to use reserved keywords as identifiers, but you still can't use characters that are not allowed in an identifier.
您可以使用@字符将保留关键字用作标识符,但仍然不能使用标识符中不允许的字符。
#4
You can add q after the question to make it descriptive. Ex:
您可以在问题后添加q以使其具有描述性。例如:
How_old_are_you_q= 0, //Replace ? with q
What_is_your_name_q= 1
#5
Another possible solution could be a mix between an enum and a dictionary :
另一种可能的解决方案可能是枚举和字典之间的混合:
public enum Question
{
Age,
Name
}
...
Dictionary<Question,string> Questions = new Dictionary<Question,string>();
Questions.Add(Question.Age, "How old are you ?");
Questions.Add(Question.Name, "What is your name ?");
#6
You can tag your Enum
items with underscore characters, which are allowed in Enum
, and replace them for display.
您可以使用下划线字符标记Enum项目,这些字符在Enum中是允许的,并替换它们以供显示。
Eg Binding an Enum
to a ComboBox
, say, replacing first "__" with "?", then "_" with space Enum
item How_old_are_you__
becomes ComboBox
item "How old are you?"
例如,将枚举绑定到ComboBox,例如,将第一个“__”替换为“?”,然后将“_”替换为空格枚举项目How_old_are_you__变为ComboBox项目“你多大了?”
I use this when I want to stick with Enum
over other options. It works well for partially loading Enum
items into lists and display controls as well.
当我想坚持使用Enum而不是其他选项时,我会使用它。它适用于将Enum项目部分加载到列表和显示控件中。