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

发布于 2024-10-03 12:35:05 字数 328 浏览 2 评论 0原文

谁能告诉我为什么我需要从我的枚举强制转换为 Int

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

如果我删除强制转换 (int) 它会失败并说我必须使用强制转换。

这是我的枚举,枚举是整数......有人知道吗?

    public enum ViewBy
    {
        Client,
        Customer
    }

Can anyone tell me why i need to cast to Int from my enum

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

If i remove the cast (int) it fails and says i must use a cast.

Here is my enum, and enums are ints.... anybody know about this?

    public enum ViewBy
    {
        Client,
        Customer
    }

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

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

发布评论

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

评论(7

极度宠爱 2024-10-10 12:35:05

在 C# 中,enum 不仅仅是数字。相反,它们是与类型相关联的数字或具有上下文中名称的数字。

为了避免在 case 语句中进行强制转换,您可以在 switch 中进行强制转换:

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

但是,这有其自身的问题。例如,这段代码会将 7 写入控制台。

enum ViewBy
{
    ChuckNorris = 1,
    JamesBond
}

Console.WriteLine((ViewBy)7);

In C# enums aren't just numbers. 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:

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.

enum ViewBy
{
    ChuckNorris = 1,
    JamesBond
}

Console.WriteLine((ViewBy)7);
铁憨憨 2024-10-10 12:35:05

您应该使用 Enum.Parse 将字符串值转换为枚举。

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

此外,枚举的基础类型不一定是 Int32。

You should convert your string value to an enum using 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.

以可爱出名 2024-10-10 12:35:05

枚举在.NET类型系统中不是int
框架借助离散整数值来存储它们 - 这是另一回事...

托马斯

Enums are NOT ints in the .NET typesystem!
The framework stores them with the aid of discrete integer values - that's a different thing...

Thomas

享受孤独 2024-10-10 12:35:05

来自 MSDN

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

每种情况下的常量表达式
标签转换为类型
表达并与
平等的表达
。控制
传递到其 case 的语句
常量表达式与值匹配
表达。

没有从 enum 到 int 的隐式转换,因此每个 case 都需要显式转换。但请注意,最初切换正确类型的表达式比转换每种情况更有意义(请参阅 Enum.Parse)。

From 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.

There is no implicit conversion from enum to int, so each of your case requires an explicit conversion. Note however that initially switching on an expression of the correct type would make more sense than casting every case (see Enum.Parse).

三五鸿雁 2024-10-10 12:35:05

我想在这种情况下看起来有点蹩脚,转换为 int 等。

假设您向该方法传递了一个 int,而不是一个枚举,请记住:

  1. 您是否会足够好地记录该内容,以便将来的任何时候您都能够修复或增强系统?
  2. 这个枚举在其他地方没有使用吗? (如何处理相同信息的模式是不犯后辈错误或迷失自我的关键。)

如果两者的答案都是“否”,那么为了自己的理智,你应该考虑保留它,不久的将来。

顺便说一句,转换为 int 并与另一个 int 进行比较比字符串更简单。

I suppose that in such case looks kind of lame, having the conversion for a int and all.

Supposing that you'd passed an int, and not a enum, to that method, keep in mind:

  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.

臻嫒无言 2024-10-10 12:35:05

回答“谁能告诉我为什么我需要从枚举转换为 Int”这个问题......

你必须转换为整数,因为枚举值不是整数。枚举具有可以存储为整数类型的基础值(默认为 int)。

来确定基础类型

Enum.GetUnderlyingType( typeof(ViewBy) );

您可以通过此处找到更多详细信息 此处

In response to the question "Can anyone tell me why i need to cast to Int from my enum"....

You have to cast to an integer because an Enums values are not integers. Enums have an underlying value that can be stored as an integral type (Default is int).

You can determine the underlying type with

Enum.GetUnderlyingType( typeof(ViewBy) );

You can find more details here and here

念﹏祤嫣 2024-10-10 12:35:05

为什么你需要强制转换为 int ?
如果 uxView.SelectedValue 是一个 ViewBy 枚举,那么简单地 (ViewBy) uxView.SelectedValue 就足够了,对吗?

Why would you need a cast to int at all?
If uxView.SelectedValue is a ViewBy enum then simply (ViewBy) uxView.SelectedValue would be enough right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文