c# - 为什么我应该将基础类型设为 Enum Int32 而不是 byte?

标签 c# .net enums

给定以下枚举:

public enum Operations_PerHourType : byte
{
    Holes = 1,
    Pieces = 2,
    Sheets = 3,
    Strips = 4,
    Studs = 5
}

当我运行 Microsoft 代码分析工具时,它告诉我:

CA1028 : Microsoft.Design : If possible, make the underlying type of 'Enums.Operations_PerHourType' System.Int32 instead of 'byte'.

它永远不会有超过几个可能的值,所以我将它声明为一个字节。为什么他们会推荐使用 int32? future 可扩展性的更多值(value)?还是有性能提升?

最佳答案

看看MSDN原因。

摘录如下:

An enumeration is a value type that defines a set of related named constants. By default, the System.Int32 data type is used to store the constant value. Even though you can change this underlying type, it is not necessary or recommended for most scenarios. Note that no significant performance gain is achieved by using a data type that is smaller than Int32. If you cannot use the default data type, you should use one of the Common Language System (CLS)-compliant integral types, Byte, Int16, Int32, or Int64 to make sure that all values of the enumeration can be represented in CLS-compliant programming languages.

关于c# - 为什么我应该将基础类型设为 Enum Int32 而不是 byte?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10216910/

相关文章:

c# - 您如何查询搜索结果中的 block ?

sql - 用于检查数组中的枚举值的 PostgreSQL 函数

java - 为什么Java不强制对枚举属性使用final

c# - 如何使用activeMQ NMS和C#实现同步请求-响应应用程序?

c# - 使用 C# 代码格式化完整地址

c# - 一次构建一个大字符串并将其传递给 response.write 或为每个片段调用 response.write 是否更有效

javascript - 使用 ajax 在本地主机内将数据从客户端发送到客户端

c# - 将一个类复制到另一个?

java - 常量特定类主体的非泛型 getClass() 方法是通过设计还是对 Java 的监督?

C# 窗体 : how do you set focus to a DataGridView component on a form?