即使指定了基础类型,也无法从枚举隐式转换值
在下面的代码示例中,我定义了一个枚举并将其基础类型指定为字节。然后,我尝试分配给字节值并打开枚举值,但收到错误:无法将类型“CmdlnFlags”隐式转换为“字节”。存在显式转换(您是否缺少强制转换?)
代码:
using System;
public enum CmdlnFlags: byte {
ValA = (byte)'a',
ValB = (byte)'b',
}
public class Sample {
public static void Main() {
byte switchByte = CmdlnFlags.ValB;
switch (switchByte) {
case CmdlnFlags.ValA: Console.WriteLine('A'); break;
case CmdlnFlags.ValB: Console.WriteLine('B'); break;
}
Console.ReadKey();
}
}
很容易修复,只需强制转换为字节即可,但是如果为枚举指定了基础类型,为什么我必须强制转换?如果无论如何都必须进行强制转换,那么指定基础类型有什么意义呢?
如果我施放,一切都会正常。例子:
byte switchByte = (byte)CmdlnFlags.ValB;
switch (switchByte) {
case (byte)CmdlnFlags.ValA: Console.WriteLine('A'); break;
case (byte)CmdlnFlags.ValB: Console.WriteLine('B'); break;
}
In the following code sample I define an enum and specify its underlying type as byte. I then attempt to assign to a byte value and switch on the enum's values but I get an error: Cannot implicitly convert type 'CmdlnFlags' to 'byte'. An explicit conversion exists (are you missing a cast?)
The code:
using System;
public enum CmdlnFlags: byte {
ValA = (byte)'a',
ValB = (byte)'b',
}
public class Sample {
public static void Main() {
byte switchByte = CmdlnFlags.ValB;
switch (switchByte) {
case CmdlnFlags.ValA: Console.WriteLine('A'); break;
case CmdlnFlags.ValB: Console.WriteLine('B'); break;
}
Console.ReadKey();
}
}
It's easy enough to fix, just cast to byte, but why do I have to cast if the underlying type is specified for the enum? What's the point of specifying an underlying type if you have to cast anyway?
If I cast, everything works. Example:
byte switchByte = (byte)CmdlnFlags.ValB;
switch (switchByte) {
case (byte)CmdlnFlags.ValA: Console.WriteLine('A'); break;
case (byte)CmdlnFlags.ValB: Console.WriteLine('B'); break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须进行投射以确保这确实是你想要做的。这是一个类型安全功能。
您应该将枚举视为与其基础类型以及具有相同基础类型的其他枚举不同的类型。它们非常不同,如果您想将其中一个用作另一个,则需要进行转换。
有时可能会很痛苦,但最终这是一件好事。
无论如何,你为什么要在切换之前进行投射呢?只需打开实际的枚举值:
在这里,您实际上并不希望将标志视为字节 - 您希望将其视为标志并打开它。所以这正是你应该做的。
You have to cast to make sure that's really what you mean to do. It's a type safety feature.
You should think of the enum as being a distinct type from its underlying type - and from other enums with the same underlying type. They're sufficiently different that if you want to use one as another, you need to cast.
It can occasionally be a pain, but ultimately it's a good thing.
Why are you casting before the switch anyway though? Just switch on the actual enum values:
Here, you don't really want to treat the flag as a byte - you want to treat it as a flag and switch on it. So that's exactly what you should do.
在大多数情况下,这样的强制转换是不必要的。无需使用
byte
类型的变量来控制开关,只需创建一个CmdlnFlags
类型的变量即可。需要强制转换来鼓励正确的程序设计。您通常不想使用枚举作为数值。
In most cases, such casts are unnecessary. Rather than use a variable of type
byte
to control the switch, just create a variable of typeCmdlnFlags
.The casts are required to encourage a correct program design. You will typically not want to use an enum as a numeric value.