据我了解,结构不是引用类型,因此仅存储为它们的值。
如果一个结构仅表示一个原始值,那么它所占用的空间是否等于仅存储该值所需要的空间?
例如,Points数组占用的内存量是否与Floats相同?
struct Point
{
float Distance;
}
Point[] Points = new Point[10];
float[] Floats = new float[10];
在此之后,这里的PointE会占用Floats的空间吗?
struct PointA
{
public float Distance;
}
struct PointB
{
public PointA Point;
}
struct PointC
{
public PointB Point;
}
struct PointD
{
public PointC Point;
}
struct PointE
{
public PointD Point;
}
void Test()
{
PointE[] PointEs = new PointE[10];
for (int i = 0; i < PointEs.Length; i++)
{
PointEs[i] = new PointE()
{
Point = new PointD()
{
Point = new PointC()
{
Point = new PointB()
{
Point = new PointA()
{
Distance = 123.456f
}
}
}
}
};
}
float[] Floats = new float[10];
for (int i = 0; i < PointEs.Length; i++)
{
Floats[i] = 123.456f;
}
// Is Floats the same size in memory as PointEs?
}