多少内存枚举走? [英] How much memory does an enum take?
问题描述
我需要决定我是否会在一个大阵,我需要在我的项目中使用枚举
秒。要做到这一点我必须知道什么是枚举
变量的大小。
枚举球员
{
首先,
第二,
三,
四
};
私人球员_owner;
_owner
将多少内存取<? / p>
我还没有发现这个正确的信息在C#中,这就是为什么这个C ++和Java主题不适:的多少内存枚举走?
枚举
值转换为在编译时的基本类型的值。因此,本身基于 INT
枚举类型类型(默认设置)的变量不使用任何更多的内存比其他任何 INT
在代码中的变量。
这是枚举
底层结构可以是任何如下:
-
字节
-
为sbyte
-
短
-
USHORT
-
INT
-
UINT
-
长
-
ULONG
由于您的播放器
枚举类型没有指定的底层时,编译器会看到它作为一个 INT
基于枚举
。如果记忆是一个问题,你可能要考虑宣布它从衍生字节
而不是:
枚举球员:字节
{
首先,
第二,
三,
四
};
不过要小心:新声明枚举
变量将具有相同的值,因为它的基本类型的默认值,这将始终为零。在与未指定的文字值枚举,在列表中的第一项被假定为默认值。在你的情况,这将是首先
:
私人球员_owner; //这个值将最初等于Player.First
您可能要插入额外的播放器
文字标记无
,空缺
或未定义
表示播放器
变量的默认值:
枚举球员:字节
{
未定义= 0;
首先,
第二,
三,
四
};
私人球员_owner; //这个值将最初等于Player.Undefined
当然,如果你是好有首先
作为默认值,您可以保留原样。要知道,虽然没有专门的默认枚举
值通常被认为是一个不好的编码实践。
作为替代方案,因为任何枚举
的结构为基础的,你也可以声明你的 _owner
变量播放器?
,使其将改为空
默认为:
专用播放器? _所有者; //这个值将最初等于null
要概括起来,只是记得枚举
文字简单地充当他们的基本类型的常量。其目的是使代码更易于阅读和执行有限的一组在编译时可能的值。
有关更多信息,请查看文档
I need to decide if I will use enum
s in a big array I need in my project. To do that I have to know what is size of an enum
variable.
enum Player
{
First,
Second,
Third,
Fourth
};
private Player _owner;
How much memory will _owner
take?
I haven't found proper information about this in C#, that's why this C++ and Java topic does not apply: How much memory do Enums take?
Enum
values are converted to values of the underlying type at compile time. Therefore, a variable of an enumeration type itself based on int
type (which is the default) doesn't use any more memory than any other int
variable in your code.
An Enum
underlying structure can be any of the following:
byte
sbyte
short
ushort
int
uint
long
ulong
Since your Player
enumeration type does not specify the underlying time, the compiler will see it as an int
-based Enum
. If memory is a concern, you may want to consider declaring it as derived from byte
instead:
enum Player : byte
{
First,
Second,
Third,
Fourth
};
Be careful though: a newly declared Enum
variable will have the same value as it's underlying type's default value, which will always be zero. In an enum with unspecified literal values, the first item in the list is assumed as the default value. In your case, that would be First
:
private Player _owner; // this value will initially equals Player.First
You may want to insert an additional Player
literal labelled None
, Empty
or Undefined
to represent a Player
variable default value:
enum Player : byte
{
Undefined = 0;
First,
Second,
Third,
Fourth
};
private Player _owner; // this value will initially equals Player.Undefined
Of course, if you are okay having First
as the default value, you may leave it as is. Be aware though that not having a dedicated default Enum
value is often considered a bad coding practice.
As an alternative, since any Enum
are structure-based, you may also declare your _owner
variable as Player?
so that it will instead be null
by default:
private Player? _owner; // this value will initially equals null
To sum it up, simply remember that Enum
literals simply acts as constants for their underlying type. Their purpose is to make code easier to read and to enforce a limited set of possible values at compile time.
For more information, look at the documentation.
这篇关于多少内存枚举走?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!