c#:为什么在 switch 语句中使用时需要从 Enum 转换为 INT?,枚举是整数
谁能告诉我为什么我需要从我的枚举强制转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 C# 中,
enum
不仅仅是数字。相反,它们是与类型相关联的数字或具有上下文中名称的数字。为了避免在
case
语句中进行强制转换,您可以在switch
中进行强制转换:但是,这有其自身的问题。例如,这段代码会将
7
写入控制台。In C#
enum
s 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 inswitch
:This, however, has its own problems. For example, this piece of code will write out
7
to the console.您应该使用 Enum.Parse 将字符串值转换为枚举。
此外,枚举的基础类型不一定是 Int32。
You should convert your string value to an enum using Enum.Parse.
Also, underlying type for an enum doesn't necessarily need to be Int32.
枚举
在.NET类型系统中不是int
!框架借助离散整数值来存储它们 - 这是另一回事...
托马斯
Enums
are NOTints
in the .NET typesystem!The framework stores them with the aid of discrete integer values - that's a different thing...
Thomas
来自 MSDN :
没有从 enum 到 int 的隐式转换,因此每个
case
都需要显式转换。但请注意,最初切换正确类型的表达式比转换每种情况更有意义(请参阅 Enum.Parse)。From MSDN :
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).我想在这种情况下看起来有点蹩脚,转换为 int 等。
假设您向该方法传递了一个 int,而不是一个枚举,请记住:
如果两者的答案都是“否”,那么为了自己的理智,你应该考虑保留它,不久的将来。
顺便说一句,转换为 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:
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"....
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
You can find more details here and here
为什么你需要强制转换为 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?