zoukankan      html  css  js  c++  java
  • 包含引用类型的值类型,复制值类型,改变其中的引用类型,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个值类型每一个都包含指向内存中同一个对象的引用(浅复制)。
  • 相关阅读:
    云时代架构读后感
    余额宝技术架构读后感
    shiyan
    11111
    编写hdfs文件遇到的问题
    123
    啦啦啦
    Hadoop安装
    js根据银行卡号进行判断属于哪个银行并返回银行卡类型
    git 使用
  • 原文地址:https://www.cnblogs.com/MrYuanly/p/5355054.html
Copyright © 2011-2022 走看看