枚举可以使用范围吗?
在 C# 中,您可以在枚举类型中使用数字范围吗,例如
public enum BookType
{
Novel = 1,
Journal = 2,
Reference = 3,
TextBook = 4 .. 10
}
编辑:需要这样做的原因是从数字转换为枚举类型,例如:
int iBook = 5
BookType btBook = (BookType)ibook
Debug.Print "Book " + ibook + " is a " btBook
预期输出是:Book 5 is a TextBook
In C#, can you use number ranges in enum types, for example
public enum BookType
{
Novel = 1,
Journal = 2,
Reference = 3,
TextBook = 4 .. 10
}
EDIT: The reason this is needed is to cast from a number to the enum type, eg:
int iBook = 5
BookType btBook = (BookType)ibook
Debug.Print "Book " + ibook + " is a " btBook
and the expected output is: Book 5 is a TextBook
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如其他人所说,不,这是不可能的。如果枚举值是标志,则可以组合它们:
As others have said, no it isn't possible. It is possible to combine enum values if they are flags:
根据 C# 标准(p612,C# 编程语言),赋予枚举的值必须是常量整数(或任何类似类型 - long、byte、sbyte、short 等),因此值范围无效。
我的编译器(VS2008)符合规范。
由于您不能在枚举中重复名称,因此您将得到的最接近的结果是这样的:
这实际上非常难看。也许枚举不能解决您的特定问题......
According to the C# standard (p612, The C# Programming Language) the value given to an enumeration must be a constant integer (or any similar type - long, byte, sbyte, short, etc), so a range of values isn't valid.
My compiler (VS2008) agrees with the spec.
Since you can't repeat names within an enumeration, the closest you'll get is something like this:
Which is actually pretty ugly. Perhaps an enum is not the solution to your particular problem ...
由于您无法为枚举分配值范围,因此我想到了另一种方法来将枚举分配为限制值(该示例基于时间):
这是假设枚举已排序,并且意外地工作带符号 (-) 值。
Since you are unable to assign a value range to an enum, I thought of an alternative approach in order to assign an enum as a limit value (the example is based on time):
This is assuming that the enum is sorted, and works unexpectedly with signed (-) values.
有点题外话,您可以使用带有只读字段的普通类来模拟枚举。
例如,与此类似的东西可以解决您的问题:
它比普通枚举更详细,但它确实允许您以类似枚举的方式定义范围成员。
例如
Somewhat offtopic, you can simulate enums using normal classes with readonly fields.
e.g. something similair to this would solve your problem:
It's quite a bit more verbose than a normal enum, but it does allow you to define ranged members in an enum like manner.
e.g.
如果您可以自己将值分配给枚举字符串,那么您可以使用一些 bitmagic 将多个 int 值映射到相同的枚举值。子类型本身可以是每个
BookType
的枚举(NovelTypes、JournalTypes 等)。缺点是,
BookType
时,它确实需要进行一些值修改,Novel = 3
稍差一些。 code> 类型的映射。示例代码:
此示例的范围为小说 16-31,期刊 32-47 等。
If you can assign the values to enum-strings yourself then you can use some bitmagic to map multiple int values to same enum value. Subtypes could be enums themselves for every
BookType
(NovelTypes, JournalTypes, etc).On the downside
BookType
Novel = 3
kind of mapping.Example code:
This example has ranges 16-31 for Novels, 32-47 for journals, etc.
不,不是。如果您尝试映射的数字常量确实具有相同的含义,那么您仍然需要为每个数字常量一个单独的成员。如TextBook4、TextBook5等。
No, it's not. If the numeric constants you're trying to map to truly have the same meaning, you would still need a separate member for each numeric constant. Like TextBook4, TextBook5, etc.
您可以选择字典。
编辑:如果字典处于类级别,您还可以将其声明为只读。
您可以使用枚举而不是字符串作为字典值。
You can opt for a dictionary rather.
Edit: You can also declare your dictionary readonly if it's in class level.
You can use an enum as the Dictionary value instead of string.
也许您想问一个相关的问题,即:对于每个值,您可以有多个枚举(不确定我的术语是否正确,但我的示例将显示我的意思)?
在 C# 中,您可以执行如下操作:
这样,您可以在代码中使用
BookType.Journal
或BookType.Magazine
,或者其中与值2
同义。是否应该这样做是另一回事 - 我不熟悉支持或反对这一点的论据(我想说“如果 C# 允许,它一定没问题”,但这会彻底疯狂)。Perhaps you meant to ask a related question, which is: can you have more than one enumeration (not sure I have the terminology correct, but my example will show what I mean) for each value?
In C#, you can do something like this:
This way, you can use either
BookType.Journal
orBookType.Magazine
in your code, either of which is synonymous with the value2
. Whether this should be done is another matter - I'm not familiar with the arguments for or against this (I'd like to say "if C# allows it, it must be OK", but that would be utterly crazy).对你的问题最简单的回答是“否”。实现你想要的最简单的方法是:
这个想法是让枚举成为单独的值并用逻辑语句处理范围。
The simplest answer to your question is No. The easiest way to accomplish what you desire is:
The idea is to let enum be individual values and handle the range with logic statements.