在枚举中指定基础类型的用例是什么?

发布于 2024-08-29 02:26:51 字数 208 浏览 4 评论 0原文

做有什么意义?

enum SomeEnum : byte // <----
{
  SomeValue = 0x01,
  ...
}

当您必须进行强制转换才能将其分配给与枚举基础类型相同类型的变量时,这样

byte b = (byte)SomeEnum.SomeValue;

What is the point of having

enum SomeEnum : byte // <----
{
  SomeValue = 0x01,
  ...
}

when you have to make a cast just to assign it to the same type of variable as the enums underlying type?

byte b = (byte)SomeEnum.SomeValue;

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

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

发布评论

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

评论(3

眼眸里的那抹悲凉 2024-09-05 02:26:51

实际上,没有什么意义,除非默认的基础类型(int)对您来说不够,即。如果您想使用比该值更高的整数值,则可以将其设为long。当您的 [Flags] 枚举包含超过 32 个值时,这会很有用。

您可以将其设置为 byteshort 只是为了限制值的范围,但当用作局部变量时,它实际上仍然需要 4 个字节(即与 int)。然而,如果用作数组,它可能占用更少的内存。

Not much point, really, except that if the default underlying type (int) is not enough for you, ie. you want to use higher integer values than that then you can make it long. This can be useful when you have a [Flags] enum with more than 32 values.

You can make it byte or short just to restrict the range of values, but it will actually still take 4 bytes when used as a local variable (ie. same as int). It may however occupy less memory if used as an array.

无远思近则忧 2024-09-05 02:26:51

来自 枚举(C# 参考)

基础类型指定多少
存储空间分配给每个
枚举器。然而,明确的强制转换
需要从枚举类型转换为
整数类型。

From enum (C# Reference)

The underlying type specifies how much
storage is allocated for each
enumerator. However, an explicit cast
is needed to convert from enum type to
an integral type.

凉城 2024-09-05 02:26:51

除了技术原因之外……这里还有一个设计原则。

将枚举指定为具有字节存储是一个实现细节。使用枚举是一个不同的问题,您不必了解或关心它的实现细节。

在客户端代码中,您使用枚举的事实应该意味着您实际上打算使用枚举,而不是字节或长等。否则为什么不只使用您想要的数据类型。

诸如 C# 之类的强类型语言致力于让您更难走出编码“契约”,这通常有助于使应用程序设计变得更好一些。

当然,我并不是说您不必参与实现细节,一个很好的例子是对象关系映射器 (ORM),您可以在其中将 C# 数据类型映射到数据库数据类型,枚举是一个很好的例子,您必须知道它的存储类型才能映射它。但在这些情况下,在我看来,必须明确地投射或反映是件好事,这在评论中是一个很好的标志,表明您在这里专门走出了通常的用法。

Apart from the technical reasons why... There is a design principle here.

specifying the enum as having byte storage is an implementation detail. Using the enum is a different issue, you should not have to know or care about the implementation details of it.

In client code, the fact that you are using an enum should mean that you are in fact meaning to use an enum, not a byte, or long etc. Otherwise why not just use the datatype you mean.

Strongly type languaged such as C# strive to make it just a little harder to step outside your coding "contracts" this usually helps make app design just that little bit better.

Now of course I am not saying that there are not times that you have to get involved in implementation details, a good example is in say an object relational mapper (ORM) where you are mapping a C# datatype to a database datatype, enums are a good example where you have to then know its storage type to map it. But in these cases, its IMO good to have to explicity cast or reflect, its a good flag in reviews that here you are specifically stepping outside the usual usage.

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