C# C语言中的枚举布尔#

C# C语言中的枚举布尔#,c#,enums,C#,Enums,我有这个方法 public enum Values { True= true, False=false }; public static string GetValues(bool values) { string status = ""; switch (values) { case(bool)UIHelper.Values.False: } } 我想把这个enum设为boolean。它说: 不能将值强制转换为布尔值 我如何才

我有这个方法

public enum Values
{
    True= true,
    False=false
};
public static string GetValues(bool values)
{
    string status = "";
    switch (values)
    {
        case(bool)UIHelper.Values.False:

    }
}
我想把这个
enum
设为
boolean
。它说:

不能将值强制转换为布尔值


我如何才能做到这一点,这样我就可以拥有它了?

当然,你可以映射到0(
服务
)和1(
串行
),但为什么首先映射到它呢?为什么不从一开始就使用bool呢

public static class UnlPointValues
{
    public const bool Serial = true;
    public const bool Service = false;
}

public static string GetUnloadingPointValues(bool values)
{
    string status = "";
    switch (values)
    {
        case UIHelper.UnlPointValues.Serial:

    }
}

0
用于
false
1
用于
true
而不是与
Convert.ToBoolean

如果值不为零,则为true;否则,错误


如果必须坚持使用
enum
,则可以实现扩展方法:


我看你不需要这里的
enum

public static string GetUnloadingPointValues(bool isSerial)
{
    return isSerial ? "Serial" : "Service";
}

或者任何要映射的
字符串
值。

0和1映射为false和true如果需要布尔值,那么为什么不坚持使用
bool
而不是enum呢?@CallumBradbury实际上是零映射到false,而不是零映射到true。@rory.ap这就是我说的bro@rory.ap实际上在C中,没有整数值映射到
bool
。我现在也该说兄弟了吗?
  public enum Values {
    True,
    False,
    // and, probably, some other options
  };

  public static class ValuesExtensions {
    public static bool ToBoolean(this Values value) {
      // which options should be treated as "true" ones
      return value == Values.False;
    }
  }
// you, probably want to check if UIHelper.Values is the same as values 
if (values == UIHelper.Values.ToBoolean()) {
  ...
}
public static string GetUnloadingPointValues(bool isSerial)
{
    return isSerial ? "Serial" : "Service";
}