In the following code example, I define an enumeration and specify its base type as a byte. Then I try to assign a byte value and include the enumeration values, but I get the error message: Cannot implicitly convert type 'CmdlnFlags' to 'byte'. An explicit conversion exists (are you missing a cast?)
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(); } }
Easy enough to fix, just pass in bytes, but why do I need to discard if the base type is specified for enumeration? What is the point of specifying the base type if you still need to cast?
If I give up, everything will work. Example:
byte switchByte = (byte)CmdlnFlags.ValB; switch (switchByte) { case (byte)CmdlnFlags.ValA: Console.WriteLine('A'); break; case (byte)CmdlnFlags.ValB: Console.WriteLine('B'); break; }
source share