简体   繁体   English

c#:当在switch语句中使用时,为什么需要从枚举到INT的转换?枚举是整数

[英]c# : Why is a cast needed from an Enum to an INT when used in a switch statement?, enums are ints

Can anyone tell me why i need to cast to Int from my enum 任何人都可以告诉我为什么我需要从我的枚举中转换为Int

        switch (Convert.ToInt32(uxView.SelectedValue))
        {
            case (int)ViewBy.Client:

If i remove the cast (int) it fails and says i must use a cast. 如果我删除了演员(int)它失败并说我必须使用演员表。

Here is my enum, and enums are ints.... anybody know about this? 这是我的枚举,枚举是整体的......任何人都知道这个?

    public enum ViewBy
    {
        Client,
        Customer
    }

In C# enum s aren't just numbers. 在C#中, enum不仅仅是数字。 Rather, they are numbers associated with the type or number with a name in a context. 相反,它们是与上下文中具有名称的类型或数字相关联的数字。

To avoid casts in case statements, you can do a cast in switch : 为了避免在case语句中使用强制转换,你可以在switch执行强制switch

switch((ViewBy)Convert.ToInt32(uxView.SelectedValue))

This, however, has its own problems. 然而,这有其自身的问题。 For example, this piece of code will write out 7 to the console. 例如,这段代码将向控制台写出7

enum ViewBy
{
    ChuckNorris = 1,
    JamesBond
}

Console.WriteLine((ViewBy)7);

You should convert your string value to an enum using Enum.Parse . 您应该使用Enum.Parse将字符串值转换为枚举。

ViewBy selected = (ViewBy) Enum.Parse(typeof(ViewBy), uxView.SelectedValue);        
switch (selected)
{
    case ViewBy.Client:
    // etc.
}

Also, underlying type for an enum doesn't necessarily need to be Int32. 此外,枚举的基础类型不一定需要是Int32。

Enums are NOT ints in the .NET typesystem! Enums 不是 ints的.NET类型系统!
The framework stores them with the aid of discrete integer values - that's a different thing... 框架在离散整数值的帮助下存储它们 - 这是另一回事......

Thomas 托马斯

From MSDN : 来自MSDN

switch ( expression )
      case constant-expression : statement
   [default  : statement]

The constant-expression in each case label is converted to the type of expression and compared with expression for equality . 每种情况标签中的常量表达式将转换为表达式,并与表达式进行相等性比较 Control passes to the statement whose case constant-expression matches the value of expression. 控制传递给case case-expression与expression的值匹配的语句。

There is no implicit conversion from enum to int, so each of your case requires an explicit conversion. 没有从枚举到int的隐式转换,因此您的每个case需要显式转换。 Note however that initially switching on an expression of the correct type would make more sense than casting every case (see Enum.Parse ). 但请注意,最初打开正确类型的表达式比铸造每个案例更有意义(请参阅Enum.Parse )。

I suppose that in such case looks kind of lame, having the conversion for a int and all. 我想在这种情况下看起来有点蹩脚,转换为int和all。

Supposing that you'd passed an int, and not a enum, to that method, keep in mind: 假设您已将int(而不是枚举)传递给该方法,请记住:

  1. Are you going to document that well enough so that any time un the future you will be able to fix or enhance the system? 您是否打算将其记录得足够好,以便在未来的任何时间您都能够修复或增强系统?
  2. Is this enum not used elsewhere? 这个枚举不在其他地方使用吗? (A pattern on how to deal with that same information is the key for not making juniors mistakes or losing yourself.) (关于如何处理相同信息的模式是不让年轻人犯错误或失去自己的关键。)

If the answer the both of the two is no, then you should consider keep it for the sake of your own sanity, in a near future. 如果答案两者都不是,那么你应该考虑在不久的将来为了你自己的理智而保留它。

And btw, is simpler to convert to int and having it to be compared with another int, than a string, per example. 顺便说一下,转换为int更简单,并将其与另一个int进行比较,而不是每个示例的字符串。

In response to the question "Can anyone tell me why i need to cast to Int from my enum".... 回答问题“任何人都可以告诉我为什么我需要从我的枚举中转换为Int”....

You have to cast to an integer because an Enums values are not integers. 您必须转换为整数,因为Enums值不是整数。 Enums have an underlying value that can be stored as an integral type (Default is int). 枚举有一个基础值,可以存储为整数类型(默认为int)。

You can determine the underlying type with 您可以使用确定基础类型

Enum.GetUnderlyingType( typeof(ViewBy) );

You can find more details here and here 您可以在此处此处找到更多详细信息

Why would you need a cast to int at all? 为什么你需要一个转换为int? If uxView.SelectedValue is a ViewBy enum then simply (ViewBy) uxView.SelectedValue would be enough right? 如果uxView.SelectedValue是一个ViewBy枚举,那么简单地(ViewBy)uxView.SelectedValue就足够了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM