C# 将 Enum 指定为 uint 的原因是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/413397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:26:46  来源:igfitidea点击:

What is the reason for specifying an Enum as uint?

c#

提问by

I came across some Enumerators that inherited uint. I couldn't figure out why anyone would do this.

我遇到了一些继承了uint. 我想不通为什么有人会这样做。

Example:

例子:

Enum myEnum : uint
{
   ...
}

Any advantage or specific reason someone would do this? Why not just leave it as defaultint?

有人会这样做有什么好处或具体原因吗?为什么不把它保留为 defaultint?

采纳答案by Hans Passant

One reason I can think of is that range checking is easier. Given that most enumerations start at 0, you'd only have to check the upper bound.

我能想到的一个原因是范围检查更容易。鉴于大多数枚举从 0 开始,您只需检查上限。

回答by Tom Anderson

if the enumeration values are greater than 2,147,483,647 and non-negative.

如果枚举值大于 2,147,483,647 且非负。

回答by Jesse Pepper

I'd guess it's been done to allow it to be automatically cast to a uint (in C, enums were treated as ints by the type system). Quite possibly to allow bitwise ORing of flag values for the enum. see http://www.velocityreviews.com/forums/t286596-enums-in-c-and-c.html

我猜已经完成了允许它自动转换为 uint (在 C 中,枚举被类型系统视为整数)。很可能允许对枚举的标志值进行按位 ORing。见http://www.velocityreviews.com/forums/t286596-enums-in-c-and-c.html

回答by Scott Willeke

It is convenient and absolutely necessary in some cases when working with COM Interop / P/Invoke from .NET. For example, you'll certainly find some at pinvoke.netdefined this way. In some cases it just maps closer to the underlying API definition, but isn't necessary, but in some cases the underlying API might use some of those high bits for flags and it will be required to be UINT. Outside of Interop, you could use some very high bits as flags too just in plain C#/VB.NET and that might also require uint.

在某些情况下,从 .NET 使用 COM Interop / P/Invoke 时,它​​很方便且绝对必要。例如,您肯定会在pinvoke.net 上找到一些以这种方式定义的内容。在某些情况下,它只是映射到更接近底层 API 定义,但不是必需的,但在某些情况下,底层 API 可能使用其中一些高位作为标志,并且需要为 UINT。在 Interop 之外,您也可以在普通的 C#/VB.NET 中使用一些非常高的位作为标志,这也可能需要 uint。

回答by Scott Willeke

The example I'm talking about is in the code here: http://www.codeplex.com/FacebookNET

我正在谈论的示例在这里的代码中:http: //www.codeplex.com/FacebookNET

Take a look at the class FacebookResponseStatus.cs

看一下类 FacebookResponseStatus.cs

I could email the author but I thought not to bother and just see what people think in the general community based on other experiences.

我可以给作者发电子邮件,但我认为不要打扰,只是看看人们根据其他经验在一般社区中的想法。

I am not going to be using this framework on CodePlex, but was reviewing it to see how others were implementing Facebook into .NET. I just thought it was interesting and wanted to know why the : uint for the Enums there.

我不会在 CodePlex 上使用这个框架,但正在审查它以了解其他人如何将 Facebook 实施到 .NET 中。我只是觉得这很有趣,并想知道为什么 : uint 用于那里的枚举。

回答by Tom Anderson

looks like they are doing it purely for this one error, which means there are probably more errors not implemented in that class.

看起来他们这样做纯粹是为了这个错误,这意味着该类中可能还有更多未实现的错误。

    /// <summary>
    /// There was an HTTP error in issuing the request.
    /// </summary>
    HttpError = 0xFFFF0000

回答by Benjol

I know of another reason, 'cos a colleague did it here.

我知道另一个原因,因为一位同事在这里做的。

To avoid the problem explained here(item 2), when generating a list of enums, he give it a unique value based on the string (I can't remember exactly how now, it can't be the hashcode 'cos that's not unique).

为了避免这里解释的问题(第 2 项),在生成枚举列表时,他根据字符串给它一个唯一值(我现在不记得到底是怎么回事,它不可能是哈希码 'cos that's that is not unique )。

The only problem was that the numbers come out a bit big, so he made the enum a uint.

唯一的问题是数字出来的有点大,所以他把枚举变成了一个 uint。

回答by Mathew Bollington

I'm converting some messaging code originally written in C into C#.

我正在将一些最初用 C 编写的消息传递代码转换为 C#。

Some fields in the C code have a limited set of possible values defined as an associated group of #defines. In other words an enumeration.

C 代码中的某些字段具有一组有限的可能值,定义为一组关联的#defines。换句话说就是枚举。

When converting some of these enumerations, some values are too large for an integer e.g.

当转换这些枚举中的一些时,一些值对于整数来说太大了,例如

#define NONE 0x00000000

#define 无 0x00000000

#define VAL1 0x00000001

#define VAL1 0x00000001

...

...

#define VALX 0x80000000

#define VALX 0x80000000

Here, VALX won't be acceptable to a default enum in C#, so the enum has to be cast as a uint, which is also the type of the storing variable in the record structure.

在这里, VALX 不会被 C# 中的默认枚举接受,因此必须将枚举转换为uint,这也是记录结构中存储变量的类型。