C# 枚举和转换

发布于 2024-07-14 15:37:23 字数 678 浏览 6 评论 0原文

如果在 C# 中声明枚举,则默认类型自动为 int。

那么为什么在 case 语句或其他实例中使用枚举时必须显式重新转换才能使用这些值呢? 如果您必须明确地说明情况,或者我只是在这里做错了什么,那么拥有基础类型有什么意义?

private enum MyEnum
        {
            Value1,
            Value2,
            Value3
        }

    switch (somevalue)
                {
                    case (int)MyEnum.Value1:
                        someothervar = "ss";
                        break;
                    case (int)MyEnum.Value2:
                        someothervar = "yy";
                        break;
                    case (int)MyEnum.Value3:
                        someothervar = "gg";
                        break;
                }

If you declare an enum in C#, the default type is automatically int.

So then why in a case statement or other instances when using the enum do you have to explicitly recast in order to use the values? What's the point of having an underlying type if you have to explicitely case or am I just doing something wrong here?

private enum MyEnum
        {
            Value1,
            Value2,
            Value3
        }

    switch (somevalue)
                {
                    case (int)MyEnum.Value1:
                        someothervar = "ss";
                        break;
                    case (int)MyEnum.Value2:
                        someothervar = "yy";
                        break;
                    case (int)MyEnum.Value3:
                        someothervar = "gg";
                        break;
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

塔塔猫 2024-07-21 15:37:23

somevalue 的类型是什么? 如果类型是 MyEnum,则不需要进行强制转换,并且应该不会出现错误。

如果类型是 int 那么是的,您将必须转换为 MyEnum 才能正确切换/大小写。 但是您可以通过转换值而不是每个 case 语句来使这变得更简单。 例如

switch( (MyEnum)somevalue )  {
  case MyEnum.Value1: ...
}

What is the type of somevalue? If the type is MyEnum casting is un-necessary and should work without error.

If the type is int then yes you will have to cast up to a MyEnum in order to properly switch / case. But you can make this a bit simpler by casting the value instead of every case statement. For example

switch( (MyEnum)somevalue )  {
  case MyEnum.Value1: ...
}
柠北森屋 2024-07-21 15:37:23

显然,某个值是一个整数,而不是显式键入为枚举。 您应该记住,枚举的基础值只是“存储类型” " 并且不能隐式互换。 但是,您可以轻松地使用强制转换运算符来使您的代码变得简单并且“更加”类型安全:

private enum MyEnum { Value1, Value2, Value3 }

switch ((MyEnum)somevalue)
{
    case MyEnum.Value1:
        someothervar = "ss";
        break;
    case MyEnum.Value2:
        someothervar = "yy";
        break;
    case MyEnum.Value3:
        someothervar = "gg";
        break;
    default:
        throw new NotSupportedException();
}

最终您会想要一种设计,您不必从整数转换为枚举,但通常在从磁盘或数据库读取时不是这种情况。

Obviously somevalue is an integer rather than explicitly typed as your enum. You should keep in mind that the underlying value of an enum is just the "storage type" and is not implicitly interchangeable. However, you can easily use the cast operator to make your code simple and "more" type safe:

private enum MyEnum { Value1, Value2, Value3 }

switch ((MyEnum)somevalue)
{
    case MyEnum.Value1:
        someothervar = "ss";
        break;
    case MyEnum.Value2:
        someothervar = "yy";
        break;
    case MyEnum.Value3:
        someothervar = "gg";
        break;
    default:
        throw new NotSupportedException();
}

Eventually you would like a design where you did not have to convert from an integer to an enum, but often times when reading from disk or DB this is not the case.

屋顶上的小猫咪 2024-07-21 15:37:23

如果 somevalue 的类型为 MyEnum,则不必强制转换为 int

public enum Color
{
    Red,
    Blue,
    Green
}

class Program
{
    static void Main(string[] args)
    {
        Color color = Color.Red;

        switch (color)
        {
            case Color.Red:
                break;

            case Color.Blue:
                break;

            case Color.Green:
                break;
        }
    }
}

If somevalue is of type MyEnum, you don't have to cast to an int.

public enum Color
{
    Red,
    Blue,
    Green
}

class Program
{
    static void Main(string[] args)
    {
        Color color = Color.Red;

        switch (color)
        {
            case Color.Red:
                break;

            case Color.Blue:
                break;

            case Color.Green:
                break;
        }
    }
}
当梦初醒 2024-07-21 15:37:23

正如其他人所说:

  • 如果某个值是 MyEnum 类型,那么您
    不应该必须施放。
  • 如果您正在从数据库中读取或
    文本文件,您可能想使用枚举
    Parse 方法,获取枚举值
    来自字符串。
  • 如果您绝对必须比较
    int,强制转换效率更高
    切换到 MyEnum,而不是
    将每个 MyEnum 值转换为 int。

As others have said:

  • If somevalue is of type MyEnum, you
    should not have to cast.
  • If you are reading from a database or
    text file, you may want to use enum's
    Parse method, to get an enum value
    from a string.
  • If you absolutely must compare an
    int, it is more efficient to cast in
    the switch to MyEnum, rather than
    cast each MyEnum value to an int.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文