无法从隐式转换枚举值,即使是指定的基本类型 [英] Unable to implicitly convert value from enum even though the underlying type is specified

查看:358
本文介绍了无法从隐式转换枚举值,即使是指定的基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码示例我定义一个枚举并指定其基础类型为字节。然后,我尝试将分配给一个字节值和枚举的值进行切换,但我得到一个错误:无法隐式转换类型'CmdlnFlags'到'字节'。一个显式转换存在(是否缺少强制转换?)



中的代码:

 使用系统; 

公共枚举CmdlnFlags:字节{
瓦拉=(字节)'A',
值Valb =(字节)'B',
}

酒店的公共类样品{
公共静态无效的主要(){
字节switchByte = CmdlnFlags.ValB;
开关(switchByte){
情况下CmdlnFlags.ValA:Console.WriteLine('A');打破;
壳体CmdlnFlags.ValB:Console.WriteLine(B);打破;
}
Console.ReadKey();
}
}



这是很容易解决,只需转换为字节,但为什么我如果枚举指定的基本类型投?什么是指定一个基础类型,如果你总得去投点?



如果我投,一切正常。例如:

 字节switchByte =(字节)CmdlnFlags.ValB; 
开关(switchByte){
的情况下(字节)CmdlnFlags.ValA:Console.WriteLine('A');打破;
的情况下(字节)CmdlnFlags.ValB:Console.WriteLine(B);打破;
}


解决方案

您必须转换为使确保真是你的意思做。这是一种安全功能



您应该考虑的枚举的作为是从它的基础类型的独特的类型 - 以及其他枚举具有相同的基本类型。他们充分不同,如果你想使用一个作为另一个,你需要施放。



这有时会带来痛苦,但最终这是一个很好的事情。



为什么你在切换之前铸造反正有关系吗?只需切换实际枚举值:

  CmdlnFlags switchFlag = CmdlnFlags.ValB; 
开关(switchFlag){
情况下CmdlnFlags.ValA:Console.WriteLine('A');打破;
壳体CmdlnFlags.ValB:Console.WriteLine(B);打破;
}



在这里,你不这样做的真正的希望对待标志作为一个字节 - 你希望把它当作一个标志和开关就可以了。所以,这正是你应该做的。


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;
        }

解决方案

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:

CmdlnFlags switchFlag = CmdlnFlags.ValB;
switch (switchFlag) {
    case CmdlnFlags.ValA: Console.WriteLine('A'); break;
    case CmdlnFlags.ValB: Console.WriteLine('B'); break;
}

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.

这篇关于无法从隐式转换枚举值,即使是指定的基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆