包含数字作为值的枚举 [英] Enum containing numbers as values
本文介绍了包含数字作为值的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
是否可以通过某种方式定义一个包含数字的枚举数?例如:
Hello,
Is it somehow possible to define an enumerator that contains numbers, too?. For example:
enum Juncture : int
{
any,
69,
1850,
contemporary,
2056,
Netherworld
}
将它们放在方括号中不起作用.我想使用此枚举器通过IsDefined()验证年龄输入.
感谢您的阅读,
Stefan
Putting them in brackets doesn''t work. I want to use this enumerator for validating input of ages using IsDefined().
Thanks for reading,
Stefan
推荐答案
编号
枚举的所有成员必须是有效的C#变量名.这意味着它们不能以数字开头,也不能包含空格,"+",=-"等.
No.
All members of an enum must be valid C# variable names. That means they can''t start with a number, and can''t contain a space, ''+'', =-'', etc.
您不能去做.就像命名变量123
一样.标识符不能以数字开头(如1abc
),但可以包含数字(如a123
).它也不能包含空格.
但是,您可以为它们分配任意序数值:
You can''t do that. That would be like naming a variable123
. An identifier cannot start with a number (as in1abc
), but it can contain numbers (as ina123
). It can''t contain spaces either.
HOWEVER, you can assign them any ordinal value you''d like:
enum Juncture { Any=0, SixtyNine=69, ReallyOld=100 };
在半相关的注释上,您可能还会发现此技巧/窍门可用:
从有问题的数据源(对于C#和VB)设置枚举器 [ ^ ]
On a semi-related note, you might also find this tip/trick usable:
Setting Enumerators From Questionable Data Sources (for C# and VB)[^]
这篇关于包含数字作为值的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文