A switch expression or case label must be bool (or ...)

c#

Solution

The error in in itself is self explanatory. it it telling you that switch expression must be ofone of these types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string. or as the C# language specification suggests

exactly one user-defined implicit conversion (§6.4) must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type corresponding to one of those types.

And you can see that BackColor is returning your a type here and it is not satisfying any of the above rules, hence the error.

you can do it like this

switch (btn.BackColor.Name)
{
   case "Green":
      break;
   case "Red":
      break;
   case "Gray":
      break;
}

Problem

Why this swich statement do not work, gives error: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type Code: ``` switch (btn.BackColor) { case Color.Green: break; case Color.Red: break; case Color.Gray: break; } ```

Original source

Related problems