How to find the size of a class in C#

前端 未结 5 627
一个人的身影
一个人的身影 2020-12-18 20:10
public class A
{
  int x;
  float y;
}

How to find the size of the class in C#. Is there any operator like Sizeof(), which used to be in C++

5条回答
  •  清歌不尽
    2020-12-18 20:48

    The following would give you the size of the C# class:

    Marshal.SizeOf(typeof(Myclass));
    
    using System.Runtime.InteropServices;
    
    [StructLayout(LayoutKind.Sequential)]
    class Myclass
    {
    
    }
    

    Remember size of a class depends on padding.

提交回复
热议问题