枚举需要多少内存? [英] How much memory does an enum take?

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

问题描述

我需要决定是否在我的项目中需要的大数组中使用枚举。为了做到这一点,我必须知道一个枚举的大小是多少。

 枚举玩家
{
首先,
二,
三,
第四
};

私人播放器_所有者;

多少内存将 _owner take? / p>

我在C#中没有找到关于这个的正确信息,这就是为什么这个C ++和Java主题不适用: Enums需要多少内存?

解决方案

在编译时,

枚举值将转换为底层类型的值。因此,基于 int 类型(这是默认值)的枚举类型本身的变量不会比任何其他 int 代码中的变量



一个枚举底层结构可以是以下任何一种:




  • byte

  • sbyte

  • short

  • ushort

  • int

  • uint

  • long

  • ulong



由于您的枚举类型没有指定基础时间,编译器会将它看作是一个 int 枚举。如果记忆是一个问题,您可能需要考虑将其声明为从 byte 派生:

 枚举播放器:字节
{
首先,
二,
三,
第四个
};

小心:新声明的枚举变量将具有与底层类型的默认值相同的值,它始终为零。在具有未指定文字值的枚举中,列表中的第一个项目被假定为默认值。在你的情况下,这将是首先

  private Player _owner; //该值最初将等于Player.First 

您可能需要插入一个额外的播放器文字标注为未定义表示 Player 变量默认值:

 枚举播放器:byte 
{
未定义= 0;
首先,
二,
三,
第四
};

私人播放器_所有者; //这个值最初将等于Player.Undefined

当然,如果你还有第一个作为默认值,你可以按原样保留。请注意,没有专门的默认值枚举值通常被认为是一种不好的编码习惯。



,因为任何枚举都是基于结构的,您还可以将 _owner 变量声明为 Player ?,以便默认为 null

 私人播放器? _所有者; //这个值最初将等于零

总结一下,只要记住枚举文字简单地作为其底层类型的常量。他们的目的是使代码更容易阅读,并在编译时强制执行一组有限的可能值。



有关更多信息,请查看文档


I need to decide if I will use enums 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屋!

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