在字节大小枚举 [英] Enum Size in Bytes
问题描述
什么是以下枚举字节大小?
what is the size of below enum in bytes?
public enum MMTPCnxNckRsn
{
MMTPCnxNckRsnNoAnswer = -2,
MMTPCnxNckRsnSendError = -1,
MMTPCnxNckRsnOk = 0,
MMTPCnxNckRsnInvalidMember = 1,
MMTPCnxNckRsnHubNotReady = 2,
MMTPCnxNckRsnUnknownMember = 3,
MMTPCnxNckRsnLastCnxTooRecent = 4,
MMTPCnxNckRsnInvalidVersion = 5,
MMTPCnxNckRsnInvalidOptions = 6,
MMTPCnxNckRsnTooManyCnx = 7
};
我用下面的代码来找到它,但我认为这是不enough..It是10 elements..Should一个字符串数组我在每一个元素算个字符,假设每个字符1个字节,并添加元素的所有字节?怎么样无符号数???
I've used the code below to find it but I think it is not enough..It is an string array with 10 elements..Should I count the chars in each element , assume each char 1 byte and add all bytes of elements? what about unsigned numbers???
var size = Enum.GetNames(typeof(MMTPCnxNckRsn)).Length;
var arr = Enum.GetNames(typeof (MMTPCnxNckRsn));
我不知道,如果它一提的是我在<$ C的中间是非常重要的$ C>编组 A机Win32代码,C#和关键的是要找到枚举在内存管理地址的大小。
I'm not sure if it is important to mention that I am in the middle of marshaling
a native win32 code to c#, and it is CRUCIAL to find the size of the enum for managing addresses in memory.
推荐答案
借助文档说:
默认基础类型的枚举元素是int型。
The default underlying type of enumeration elements is int.
因此,你的数据类型将有4个字节的大小,这是一个int的大小
您可以用下面的命令证实了这一点:
Therefore, your data type will have the size of 4 bytes, which is the size of an int.
You can confirm this by using the following command:
Marshal.SizeOf(Enum.GetUnderlyingType(typeof(MMTPCnxNckRsn)));
虽然你使用的名称是指在源代码中的枚举值,它们的代码来表示的运行本机作为整数值上。因此你对字符串数组的评论是很离谱的。
Whilst you refer to the enum values in source code using a name, they are represented in the code that runs on the machine as integer values. So your comment about string arrays is quite wide of the mark.
我也认为你是在思维大小的问题。看着你最近的其他问题,你似乎是一个翻译C ++结构,C#进行的PInvoke。那么,C#枚举将直接映射到C ++枚举。该PInvoke的编组看起来大小后,奠定他们为你。你并不需要处理明确。
I also think you are over-thinking the issue of size. Looking at your other recent question, you seem to be translating a C++ structure to C# for pinvoke. Well, the C# enum will map straight onto the C++ enum. The pinvoke marshaller will look after the sizes and lay them out for you. You do not need to handle that explicitly.
这篇关于在字节大小枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!