我们都知道:
结构体是值类型,一般在栈上存储;
类是引用类型,对象存储在堆内存中;
如果结构体中包含类对象,则结构体持有的是堆内存中对象的引用:
class ClassTest
{
public int x;
public int y;
public int z;
}
struct StructTest
{
public ClassTest classTest; //对象的引用,默认为null
public int x;
}
如果类中包含结构体,则类似普通数据(如 int),结构体直接保存在对象中,即保存在堆内存中:
struct StructTest2
{
public int x;
public int y;
public int z;
}
class ClassTest2
{
public int x;
public StructTest2 structTest; //完整的结构体数据,类似int x
}