包含引用类型的值类型,复制值类型,改变其中的引用类型,2个值类型中的引用类型都将改变。

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个值类型每一个都包含指向内存中同一个对象的引用(浅复制)。

 

posted @ 2016-04-05 14:47  浅言  阅读(222)  评论(0编辑  收藏  举报