Enum Size in Bytes

前端 未结 1 1731
我寻月下人不归
我寻月下人不归 2020-12-03 21:23

What is the size of the enum below in bytes?

public enum MMTPCnxNckRsn 
{
    MMTPCnxNckRsnNoAnswer = -2, 
    MMTPCnxNckRsnSendError = -1, 
    MMTPCnxNckRsn         


        
相关标签:
1条回答
  • 2020-12-03 22:24

    The documentation says:

    The default underlying type of enumeration elements is 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.

    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.

    0 讨论(0)
提交回复
热议问题