namespace structTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=====第一次输出=====");
Rectangle r1=new Rectangle("第一次",1,2,3,4);
Console.WriteLine("=====第二次输出=====");
Rectangle r2 = r1;
Console.WriteLine("=====这一次改编值=====");
r2.RectInfo.infoString = "改变之后";
r2.rectBottom = 333;
r1.Dispaly();
r2.Dispaly();
}
}
class ShapeInfo
{
public string infoString;
public ShapeInfo(string info)
{
infoString = info;
}
}
struct Rectangle
{
public ShapeInfo RectInfo;
public int rectTop, rectLeft, rectBottom, rectRight;
public Rectangle(string info, int rectTop, int rectLeft, int rectBottom, int rectRight)
{
RectInfo = new ShapeInfo(info);
this.rectTop = rectTop;
this.rectLeft = rectLeft;
this.rectBottom = rectBottom;
this.rectRight = rectRight;
}
public void Dispaly()
{
Console.WriteLine("String={0},Top={1},Left={2},Bottom={3},Right={4}", RectInfo.infoString, rectTop, rectLeft, rectBottom, rectRight);
}
}
}
默认情况下,当值类型包含其他引用类型时,赋值将生成一个引用的副本。2个值类型每一个都包含指向内存中同一个对象的引用(浅复制)。