C#你真的懂了吗 - 啥叫引用

看看下面的这段代码,如果你得到的结果和实际情况一致,那就是明白引用类型了

    class Program
    {
        static void Main(string[] args)
        {
            Car theCar = new Car();
            TestClass(theCar);
            Console.WriteLine(theCar.Name);
            TestClass2(theCar);
            Console.WriteLine(theCar.Name);
            TestClass3(theCar);
            Console.WriteLine(theCar.Name);
            Console.ReadLine();

        }

        public static void TestClass(Car mycar)
        {
            mycar.Name = "aaa";
            mycar.Number = 100;
            mycar.ProductArea = "china";
        }

        public static void TestClass2(Car mycar)
        {
            Car thiscar = mycar;
            thiscar.Name = "bbb";
            thiscar.Number = 200;
            thiscar.ProductArea = "Japan";
        }

        public static void TestClass3(Car mycar)
        {
            Car thiscar = new Car(mycar);
            thiscar.Name = "ccc";
            thiscar.Number = 300;
            thiscar.ProductArea = "Korea";
        }
    }

    class Car
    {
        public string Name; 
        public string ProductArea;
        public int Number;

        public Car(Car thecar)
        {
            this.Name = thecar.Name;
            this.Number = thecar.Number;
            this.ProductArea = thecar.ProductArea;
        }
        public Car()
        {
            this.Name = null;
            this.Number = 0;
            this.ProductArea = null;
        }
    }

得到的结果是:

aaa
bbb
bbb

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值