c# - 为什么 WinDbg 显示 C# 类和结构的大小相同?

标签 c# windbg

首先,我问了有关类(class)规模的问题,我得到了答案,我也明白了这一点。 现在,当我看到 WinDbg 显示相同类型的类和结构的相同大小时,我感到很困惑,因为我读到结构不包含 SyncBlock,那么结构的大小应该比类的大小少 8 个字节。

我的第一个问题:

结构体没有 SyncBlock 字段是否正确?

以下是我的类(class):

public class MyClass
{
public int Age { get; set; }
public double Amount { get; set; }
public string Name { get; set; }
}

结构是:

public struct MyStruct
{
public int Age { get; set; }            
public double Amount { get; set; }                  
public string Name { get; set; }
}

使用 WinDbg,我得到 MyClass 和 MyStruct 的 size = 40

对于类我理解它是因为它有:

SyncBlock  = 8 bytes 
TypeHandle = 8 bytes
String ref = 8 bytes
Int32      = 4 bytes
Double     = 8 bytes
Total      = 36 bytes

由于它需要选择 8 字节边界,因此它变为 40 字节,但为什么结构也是 40 字节?

我在 64 位系统上运行。 以下是 WinDbg 的输出: enter image description here enter image description here

最佳答案

好吧,我运行了以下代码:

[TestMethod]
public void TestSize()
{
    MyStruct s = new MyStruct();
    Debug.Print(Marshal.SizeOf(s).ToString());
}

得到了预期的结果24。

关于c# - 为什么 WinDbg 显示 C# 类和结构的大小相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101584/

相关文章:

c# - 获取文件的相对路径?

c# - 使用 NUnit,我可以确定哪些测试先于当前运行的测试吗?

c# - 使用 Knockout 从 <select> 填充文本框

windows - <unclassified> 在 windbg !address 输出中是什么意思

debugging - "ntsd"的 !DumpHeap 命令从网上下载到底是什么?

c++ - 堆损坏双重释放内存

c# - 无法使转换器工作

c# - AspNet Core 中 UTC 的 Http 查询参数

debugging - 调试本地进程时 WinDbg 变慢(跳过)

stack - 测试多个堆栈变量的条件断点