实例中的值类型在哪里

Where goes a Value type in an instance

众所周知,值类型是以栈的形式存储的。现在考虑一下:

class test
{
    public int Sum { get; set; }
    public string Name { get; set; }
}

然后创建实例如下:

test t = new test();
t.Sum = 10;
t.Name = "a string";

现在,实例及其属性将如何存储在堆中? 因为我们有一个 int 应该存储在堆栈中。

As we know value types are stored in stack.

更好的表达方式是"value types can be stored in the stack"。实际放置取决于上下文。

值类型的对象仅当您将这些对象创建为局部变量时才存储在堆栈中。当您使值类型对象成为其他对象的一部分时,值类型对象将成为其 "owner" 对象的一部分。根据所有者对象类型,如果 test 被声明为 struct.

,则值类型对象将放置在动态内存中,如您的示例或堆栈中