ref用于类类型

 

        static void Main(string[] args)         {            

 

            Fruit f = new Fruit()      {                

    Weight = 1,                

    Color = ""            

    };

 

            Change(ref f);            

//把引用对象作为参数传进一个方法,实际上是在栈上新分配了一块内存保存传入的地址

    WriteLine(f.Weight); //output is 2

   }

 

       static void Change(ref Fruit f)
        {
            f.Weight = 2;
            f = new Fruit() { Weight = 3 };
        }

 

https://www.cnblogs.com/lwzz/archive/2012/03/11/2390589.html

 

posted @ 2018-11-14 16:18  Marco CAO  阅读(1281)  评论(0编辑  收藏  举报