枚举背后int的基本原理 [英] Rationale behind int to enum casts

查看:112
本文介绍了枚举背后int的基本原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


花半小时搜索档案我还没有找到以下行为的理由




使用系统;


//注意缺少的Flags属性

enum颜色

{

红色,

绿色,

蓝色

}


类无论如何

{

静态无效主要()

{

//以下演员成功虽然

//值42不存在。

颜色=(颜色)42;

Console.WriteLine(" {0}",颜色);

}

}


我相信如果那个演员能够让我和其他人的生活变得更轻松>
失败。此外,该框架似乎甚至没有提供直接实现这一功能的功能

。我必须自己滚动,使用

Enum.IsDefined()。


这是什么理由?


谢谢&此致,


Andreas

解决方案

试着看看IConvertible


Andreas Huber <啊**** @ gmx.net> schrieb im Newsbeitrag

新闻:3e ************************** @ posting.google.c om ...

你好

花半小时搜索档案我还没有找到以下行为的理由。
使用系统;

//注意缺少的Flags属性
枚举颜色
{
红色,
绿色,
蓝色}

类无论什么
{
static void Main()
//
//以下演员成功虽然
//值42不存在。
颜色=(颜色)42;
Console.WriteLine(" {0}",color);
}
}

我相信如果那个演员失败,它会让我和其他人的生活更轻松。而且,该框架似乎甚至没有提供直接实现这一功能的功能。我必须自己动手,使用
Enum.IsDefined()。

这是什么理由?

谢谢&此致,

Andreas



嗨Andreas,


你的枚举真的是一个int。

由于性能问题,框架不会被看作是否定义

问题。

想象一下这个

enum Tubo

{

Alfa = 11,

Beta = 44

}


public void SomeMethod(int j)

{

int i = 5 * j;

吐蕃t =(吐蕃)i;

}


如果运行时检查它是否有效,那将是一个很大的性能损失。 。


-

Miha Markic - RightHand .NET咨询&软件开发

miha at rthand com


" Andreas Huber" <啊**** @ gmx.net>在消息中写道

news:3e ************************** @ posting.google.c om ...

你好

花半小时搜索档案我还没有找到以下行为的理由。
使用系统;

//注意缺少的Flags属性
枚举颜色
{
红色,
绿色,
蓝色}

类无论什么
{
static void Main()
//
//以下演员成功虽然
//值42不存在。
颜色=(颜色)42;
Console.WriteLine(" {0}",color);
}
}

我相信如果那个演员失败,它会让我和其他人的生活更轻松。而且,该框架似乎甚至没有提供直接实现这一功能的功能。我必须自己动手,使用
Enum.IsDefined()。

这是什么理由?

谢谢&此致,

Andreas



2003年11月26日星期三14:49:38 + 0100,Miha Markic < miha at rthand com>

写道:

如果运行时检查它是否有效,那将是一个很大的性能损失......




只有当你经常从int转换为enum时,你不应该这样做b $ b无论如何都要做因为它打败了enum第一个

的地方。至少在调试模式下,编译器或框架应自动检查

该值是否实际有效。


再次,使用枚举而不是使用枚举的全部意义ints是类型安全的。

我同意Andreas的说法,这种行为很奇怪,应该是固定的,b / b是否有效。

-
http://www.kynosarges.de


Hi there

Spending half an hour searching through the archive I haven''t found a
rationale for the following behavior.

using System;

// note the missing Flags attribute
enum Color
{
Red,
Green,
Blue
}

class Whatever
{
static void Main()
{
// The following cast succeeds although the
// value 42 does not exist.
Color color = (Color) 42;
Console.WriteLine( "{0}", color );
}
}

I believe it would make my and other peoples life easier if that cast
failed. Moreover, the framework does not even seem to offer a function
to implement this directly. I have to roll my own, using
Enum.IsDefined().

What is the rationale for this?

Thanks & Regards,

Andreas

解决方案

Try to look at IConvertible

"Andreas Huber" <ah****@gmx.net> schrieb im Newsbeitrag
news:3e**************************@posting.google.c om...

Hi there

Spending half an hour searching through the archive I haven''t found a
rationale for the following behavior.

using System;

// note the missing Flags attribute
enum Color
{
Red,
Green,
Blue
}

class Whatever
{
static void Main()
{
// The following cast succeeds although the
// value 42 does not exist.
Color color = (Color) 42;
Console.WriteLine( "{0}", color );
}
}

I believe it would make my and other peoples life easier if that cast
failed. Moreover, the framework does not even seem to offer a function
to implement this directly. I have to roll my own, using
Enum.IsDefined().

What is the rationale for this?

Thanks & Regards,

Andreas



Hi Andreas,

Your enum is really an int.
And the framework won''t look if it is defined or not because of performance
issue.
Imagine this

enum Tubo
{
Alfa = 11,
Beta = 44
}

public void SomeMethod(int j)
{
int i = 5 * j;
Tubo t = (Tubo)i;
}

It would be a big performance hit if runtime checks if it is valid or not...

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Andreas Huber" <ah****@gmx.net> wrote in message
news:3e**************************@posting.google.c om...

Hi there

Spending half an hour searching through the archive I haven''t found a
rationale for the following behavior.

using System;

// note the missing Flags attribute
enum Color
{
Red,
Green,
Blue
}

class Whatever
{
static void Main()
{
// The following cast succeeds although the
// value 42 does not exist.
Color color = (Color) 42;
Console.WriteLine( "{0}", color );
}
}

I believe it would make my and other peoples life easier if that cast
failed. Moreover, the framework does not even seem to offer a function
to implement this directly. I have to roll my own, using
Enum.IsDefined().

What is the rationale for this?

Thanks & Regards,

Andreas



On Wed, 26 Nov 2003 14:49:38 +0100, "Miha Markic" <miha at rthand com>
wrote:

It would be a big performance hit if runtime checks if it is valid or not...



Only if you constantly convert from int to enum which you shouldn''t be
doing anyway because it defeats the point of having enums in the first
place. At least in debug mode, the compiler or framework should check
automatically that the value is actually valid.

Again, the whole point of using enums instead of ints is type safety.
I agree with Andreas that this behaviour is bizarre and should be
fixed, performance hit or not.
--
http://www.kynosarges.de


这篇关于枚举背后int的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆